如何使用帶有用戶定義類型的std :: maps作為鍵?我想知道為什么我不能將STL映射與用戶定義的類一起使用。當(dāng)我編譯下面的代碼時(shí),我收到以下神秘的錯(cuò)誤消息。這是什么意思?此外,為什么它只發(fā)生在用戶定義的類型?(當(dāng)它們用作密鑰時(shí),原始類型是可以的。)C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.5 ........ \ include \ c ++ \ 3.4.5 \ bits \ stl_function.h ||在成員函數(shù)`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 ::實(shí)例化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 |從此處實(shí)例化|C:\ MinGW \ bin .. \ lib \ gcc \ mingw32 \ 3.4.5 ........ \ include \ c ++ \ 3.4.5 \ bits \ stl_function.h | 227 |錯(cuò)誤:不匹配'運(yùn)算符<'in'__ x <__y'| || ===構(gòu)建完成:1個(gè)錯(cuò)誤,0個(gè)警告=== |#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 關(guān)注
- 451 瀏覽
添加回答
舉報(bào)
0/150
提交
取消