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

轉載自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)));

個人感想:當時也是同樣的編譯錯誤,編譯器已經很明顯了,提示構造函數形式不對,當時應該仔細分析一下
也應該可以想到的。以後得多多分析問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章