C++ 什么是 "instantiated from here"错误?

标签 c++ struct std

我是 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/

相关文章:

c++ - 是否定义了减去两个 NULL 指针的行为?

C++ 错误 : no match for 'operator>>' in 'input >> Group1->Entrepreneur::Item' |

c++函数指针和void指针交互导致奇怪的事情

c++ - 当给定字符串时,Cout 从行首开始打印

c - malloc() 在无限循环中

c++ - 无法 typedef std::chrono::microseconds 覆盖其定义以更改其基础类型

c++ - Qt 菜单栏不显示

c++ - 递归互斥锁背后的想法

c - 将 char *c[] 分配给结构中的双指针

在另一个结构中创建结构数组