我是 C++ 编程的新手,我想知道什么是“从此处实例化”错误?
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);
我收到几条错误消息
- 没有匹配函数来调用“Data::Data()”
- “从这里实例化”错误
谢谢。
最佳答案
struct Data
没有默认构造函数。 map::operator[]
返回其值类型的默认构造实例,在本例中为 struct Data
。
要么提供默认构造函数:
Data() : x(0), y(0) {}
或使用 std::map::insert()
:
m.insert(std::pair<int, Data>(1, Data(1, 2)));
关于C++ 什么是 "instantiated from here"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996141/