简体   繁体   English

C ++什么是“从这里实例化”错误?

[英]C++ what is “instantiated from here” error?

I am new to C++ programming, I am wondering what is "instantiated from here" error? 我是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);

I got several error messages 我收到了几条错误消息

  • no matching function for call to "Data::Data()" 没有用于调用“Data :: Data()”的匹配函数
  • "instantiated from here" error “从这里实例化”错误

Thanks. 谢谢。

There is no default constructor for struct Data . struct Data没有默认构造struct Data The map::operator[] returns a default constructed instance of its value type, in this case struct Data . map::operator[]返回其值类型的默认构造实例,在本例中为struct Data

Either supply a default constructor: 提供默认构造函数:

Data() : x(0), y(0) {}

or use std::map::insert() : 或者使用std::map::insert()

m.insert(std::pair<int, Data>(1, Data(1, 2)));

C++ what is “instantiated from here” error? C ++什么是“从这里实例化”错误?

That is not an error, but the continuation of the previous error adding extra information. 这不是错误,而是继续添加额外信息的先前错误。 The compiler is adding the error: prefix so that it is easier to read (or parse) what lines belong to the error. 编译器正在添加error:前缀,以便更容易读取(或解析)属于错误的行。

You can read the whole block as a single error: 您可以将整个块读作单个错误:

No matching function call to Data::Data() instantiated from... 没有匹配的函数调用Data::Data()从...实例化

您需要提供一个不带参数的构造函数。

Data::Data(){}

这意味着只有当编译器开始解析( 实例化 )模板std::map::map()时,错误(“无匹配函数调用”)才变得明显。

When you insert an item into a map using operator[] , what happens is that the map inserts a default-constructed object for the specified key to refer to, then the value you're assigning gets copied into that default constructed object. 当您使用operator[]将项目插入到地图中时,会发生的情况是地图会插入默认构造的对象以供指定的键引用,然后您分配的值将被复制到该默认构造对象中。

To create that default-constructed object, a default constructor needs to be available. 要创建默认构造的对象,需要使用默认构造函数。 In your case, sine you've specified a constructor that takes two arguments (and not supplied default values for those arguments), the compiler will not automatically synthesize a default constructor for you. 在你的情况下,正弦你已经指定了一个带有两个参数的构造函数(并没有为这些参数提供默认值),编译器不会自动为你合成一个默认的构造函数。

Under the circumstances, I'd probably modify your constructor to something like this: 在这种情况下,我可能会修改你的构造函数,如下所示:

Data(int a=0, int b=0) : x(a), y(b) {}

Note that you should generally prefer initializing variables in an initialization list (if possible) over assigning to them in the body of the constructor. 请注意,您通常应该首先在初始化列表中初始化变量(如果可能),而不是在构造函数的主体中分配它们。

no matching function for call to "Data::Data()" 没有用于调用“Data :: Data()”的匹配函数

You need to provide a default constructor. 您需要提供默认构造函数。 See the answer to this question: 看到这个问题的答案:

Template Error: no appropriate default constructor available 模板错误:没有适当的默认构造函数可用

"instantiated from here" error “从这里实例化”错误

You can only have variable declarations in the global area. 您只能在全局区域中拥有变量声明。 You need do that assignment from within a function. 您需要在函数内执行该分配。 Correct code: 正确的代码:

#include <map>

struct Data {
    Data(int a, int b) {
        x = a;
        y = b;
    }
    int x;
    int y;

    Data() {};
};

std::map<int, Data> m;

void main()
{
   m[1] = Data(1, 2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM