转载自Stack Overflow。
struct Data{
Data(int a,int b){
x = a;
y = b;
}
int x;
int y;
}
std::map<int,Data> m;
m[1] = Data(1,2);
compile error:
1: no matching function for call to "Data::Data()"
2: "instantiated from here" error;
Answers:There is no default constructor for struct Data.
The map::operator[] returns a default constructed instance of its value type, in this case struct Data.
Either supply a default constructor:
Data() : x(0), y(0) {}
or use std::map::insert():
m.insert(std::pair<int, Data>(1, Data(1, 2)));
wjy123
个人感想:当时也是同样的编译错误,编译器已经很明显了,提示构造函数形式不对,当时应该仔细分析一下
也应该可以想到的。以后得多多分析问题。