Hi, I'm trying to use a map with a string key to store a user defined class and seem to be running into problems. I'm using g++ and am getting a big barf of errors:
1 2 3 4 5 6 7 8 9 10 11
/usr/include/c++/4.0.0/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = A, _Compare = std::less, _Alloc = std::allocator >]’:
main.cpp:50: instantiated from here
/usr/include/c++/4.0.0/bits/stl_map.h:339: error: no matching function for call to ‘A::A()’
main.cpp:17: note: candidates are: A::A(std::string)
main.cpp:15: note: A::A(const A&)
make: *** [main.o] Error 1
If I comment out the "A b = mymap[s];" line, everything is fine, but if I try any code that uses the [] operator I get the same general compiler error. I'm sure this is just some gotcha I have to get through, can anyone help?
because operator[] has the side effect of creating a new key-value pair if the key being sought after does not exist. because there is no value specified for the given key, it has to default construct the value.
@helios: While it is working, but what I don't understand is, why is A b = mymap[s] ; not calling the copy constructor?
As map's value is of type A and technically, mymap[s] should return A. Right?
That line doesn't call the copy constructor. It first constructs b without parameters and then calls A::operator=(). If A::operator=() is not defined, the compiler defines one of its own that shallow-copies the right-hand operand into the left-hand operand.
@helios: I am sorry if I sound stupid, but I think I did not understood what you said: As if I am not wrong then, since A b = mymap[s]; at this point b has been declared right. So consider this code
#include<iostream>
usingnamespace std;
class A {
public:
A() { cout<<"Constructor"<<endl;
}
A(A& o) {
cout<<"Copy Constructor"<<endl;
}
};
int main() {
A a;
A b = a; // This will call copy constructor
return 0;
};
A b = a; will be valid because I have not specified "explicit keyword" in front of my copy constructor. So in the above code, line A b = a; will call copy constructor and not default constructor. And I see the similar relationship in the code mentioned in the first post. Could you please point where I am going wrong ?