【发布时间】:2010-11-06 18:28:07
【问题描述】:
使用 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 标准::运算符 &,const std::_Tree<_traits> &)' :不能 推导出 'const 的模板参数 std::_Tree<_traits> &' 来自 'const MyKeyObject' c:\程序文件\微软 视觉工作室 8\vc\include\function 143
错误 2 错误 C2784: 'bool 标准::运算符 &,const _Elem *)' : 无法推断 'const 的模板参数 std::basic_string<_elem> &' 来自 'const Tasking::MyKeyObject' c:\program 文件\微软视觉工作室 8\vc\include\function 143
错误 3 错误 C2784: 'bool std::operator &)' : 无法推导出模板 'const _Elem *' 的参数来自 'const MyDataObject' c:\program 文件\微软视觉工作室 8\vc\include\功能 143
...
如果我将键设置为像 int 这样简单的东西,一切都很好。
我做错了什么?!也许我需要用模板做点什么?
有没有更好(更快?)的方式来使用这样的自定义键对象访问数据?
【问题讨论】:
-
hash_map是一个旧的扩展。使用 tr1 的unordered_map或 Boost。