使用 STL C++ hash_map...
class MyKeyObject
{
std::string str1;
std::string str2;
bool operator==(...) { this.str1 == that.str1 ... }
};
class MyData
{
std::string data1;
int data2;
std::string etcetc;
};
像这样...
MyKeyObject a = MyKeyObject(...);
MyData b = MyData(...);
stdext::hash_map <MyKeyObject, MyData> _myDataHashMap;
_myDataHashMap[ a ] = b;
我收到一大堆错误。这是前三个...
错误 1 错误 C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : 无法推断出 'const std::_Tree<_Traits> 的模板参数&' 来自 'const MyKeyObject' c:\program files\microsoft visual studio 8\vc\include\functional 143
错误 2 错误 C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : 无法推断 'const std::basic_string<_Elem,_Traits, _Alloc> &' 来自 'const Tasking::MyKeyObject' c:\program files\microsoft visual studio 8\vc\include\functional 143
错误 3 错误 C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : 无法从 'const MyDataObject' c 推导出 'const _Elem *' 的模板参数:\程序文件\微软视觉工作室 8\vc\include\functional 143
...
如果我将键设置为像 int 这样简单的东西,一切都很好。
我究竟做错了什么?!也许我需要用模板做点什么?
有没有更好(更快?)的方式来使用这样的自定义键对象访问数据?