问题
I'm getting the error:
error: no matching function for call to 'A::A()'
note: candidates are: A::A(const A&)
note: A::A(const std::string&, size_t)
From this:
#include <map>
#include <string>
using std::map;
using std::string;
class A {
public:
string path;
size_t size;
A (const string& p, size_t s) : path(p), size(s) { }
A (const A& f) : path(f.path), size(f.size) { }
A& operator=(const A& rhs) {
path = rhs.path;
size = rhs.size;
return *this;
}
};
int main(int argc, char **argv)
{
map<string, A> mymap;
A a("world", 1);
mymap["hello"] = a; // <----- here
A b(mymap["hello"]); // <----- and here
}
Please tell me why the code wants a constructor with no parameters.
回答1:
mymap["hello"] can attempt to create a value-initialized A, so a default constructor is required.
If you're using a type T as a map value (and plan to access value via operator[]), it needs to be default-constructible - i.e. you need a parameter-less (default) constructor. operator[] on a map will value-initialize the mapped value if a value with the key provided is not found.
回答2:
Because map requires DefaultConstructible values, since when using subscript operator and the key is not found it adds it mapped to a default constructed value.
回答3:
In general a default constructor is not required for a map item value.
It was required in C++03, but that requirement was dropped in C++11, which often instead of posing container-wide requirements uses a more fine grained requirement scheme with requirements on the use of particular member functions.
The map::operator[] is one such member function, because it will create an item with the given key and a default-constructed value, if an item with the key doesn't exist.
This is also the reason why there isn't a const version of map::operator[]: it potentially modifies the map.
In C++11 and later you can use the at accessor to access an item with a given key in a const map, without the verbosity & complexity of a find.
Since map::at doesn't attempt to create an item, it doesn't require the item type to be default constructible.
And so one practical solution for your problem is to use map::at instead of map::operator[].
回答4:
Long time without using C++, but If I recall correctly if you don't define a constructor for a class the compiler will create a paramless one for you. As soon as you define a constructor with parameters the compiler won't create a paramless one for you, so, you are required to create one. This added to what K-ballo exposed leads to your errors.
来源:https://stackoverflow.com/questions/14164223/why-is-a-default-constructor-required-when-storing-in-a-map