如何使用帶有用戶定義類型的std :: maps作為鍵?我想知道為什么我不能將STL映射與用戶定義的類一起使用。當我編譯下面的代碼時,我收到以下神秘的錯誤消息。這是什么意思?此外,為什么它只發生在用戶定義的類型?(當它們用作密鑰時,原始類型是可以的。)C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.5 ........ \ include \ c ++ \ 3.4.5 \ bits \ stl_function.h ||在成員函數`bool std :: less <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = Class1]':C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.5 ........ \ include \ c ++ \ 3.4.5 \ bits \ stl_map.h | 338 |從`_Tp&std ::實例化map <_Key,_Tp,_Compare,_Alloc> :: operator [](const _Key&)[with _Key = Class1,_Tp = int,_Compare = std :: less,_Alloc = std :: allocator>]'|C:\ Users \ Admin \ Documents \ dev \ sandbox \ sandbox \ sandbox.cpp | 24 |從此處實例化|C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.5 ........ \ include \ c ++ \ 3.4.5 \ bits \ stl_function.h | 227 |錯誤:不匹配'運算符<'in'__ x <__y'| || ===構建完成:1個錯誤,0個警告=== |#include <iostream>#include <map>using namespace std;class Class1{public:
Class1(int id);private:
int id;};Class1::Class1(int id): id(id){}int main(){
Class1 c1(1);
map< Class1 , int> c2int;
c2int[c1] = 12;
return 0;}
- 3 回答
- 0 關注
- 457 瀏覽
添加回答
舉報
0/150
提交
取消