Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ what is "instantiated from here" error?

Tags:

c++

std

struct

I am new to C++ programming, I am wondering what is "instantiated from here" error?

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()"
  • "instantiated from here" error

Thanks.

like image 301
2607 Avatar asked Dec 12 '22 03:12

2607


1 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)));
like image 107
hmjd Avatar answered Dec 29 '22 18:12

hmjd