rahman
rahman

Reputation: 4948

error: no matching function for call

Can any body please help me solve this problem. I have a class Comm that stores elements of another class Info in its map container:

    #include<map>
using namespace std;
class En
{

};

class Info
{
public:
    const En * en;
    bool read;
    bool write;
    bool done;
    Info(
            En * en_,
            bool read_,
            bool write_,
            bool done_
            )
    :
        en(en_),
        read(read_),
        write(write_),
        done(done_)
    {}

    Info(const Info& info_)
    :
        en(info_.en),
        read(info_.read),
        write(info_.write),
        done(info_.done)
    {}


};

class Comm
{
    std::map<const En*,Info> subscriptionList;
public:
void  subscribeEn(Info value)
{
    //none of the below works
//  subscriptionList[value.en] = Info(value);
    subscriptionList[value.en] = value;
}
};


int main()
{

//  En * en;
//  bool read;
//  bool write;
//  bool Done;
//  Comm comm;
//  Info Info_(en,read,write,Done);
//  comm.subscribeEn(Info_);
//  return 1;

}

but I get the following error in compilation:

In file included from /usr/include/c++/4.7/map:61:0,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/stl_map.h: In instantiation of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = const En*; _Tp = Info; _Compare = std::less<const En*>; _Alloc = std::allocator<std::pair<const En* const, Info> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Info; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = const En*]’:
test.cpp:47:27:   required from here
/usr/include/c++/4.7/bits/stl_map.h:458:11: error: no matching function for call to ‘Info::Info()’
/usr/include/c++/4.7/bits/stl_map.h:458:11: note: candidates are:
test.cpp:28:2: note: Info::Info(const Info&)
test.cpp:28:2: note:   candidate expects 1 argument, 0 provided
test.cpp:15:2: note: Info::Info(En*, bool, bool, bool)
test.cpp:15:2: note:   candidate expects 4 arguments, 0 provided

I appreciate if you let me know why I get this and how to solve it. thank you

Upvotes: 0

Views: 4076

Answers (2)

Spook
Spook

Reputation: 25927

I believe, that problem is here:

class Comm
{
    std::map<const En*,Info> subscriptionList;
public:
    void  subscribeEn(Info value)
    {
        //none of the below works
        //  subscriptionList[value.en] = Info(value);
        subscriptionList[value.en] = value;
    }
};

I guess, that std::map first instantiates a pair with const En* and Info and then makes an assignment to fields of this pair. You haven't provided a parameterless constructor for Info and that's why compiler complains.

You can solve this by adding the following to your Info class:

// Default, parameterless constructor
Info()
{
    // Some default values
    en = NULL; // or nullptr in C++11
    read = false;
    write = false;
    done = false;
}

Another solution is to change definition of std::map, such that it contains a pointer to Info rather than its instance:

std::map<const En *,Info *> subscriptionList;

Upvotes: 5

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

Incorrect capitalization of done:

bool Done;
Info Info_(en,read,write,done);

Typically lowercase or camelCase are used for variable names.

Upvotes: 2

Related Questions