我正在尝试在C ++中实现字典,但是当我定义类哈希的特殊化时,会出现错误:
error: 'hash' is not a class template
error: template argument required for 'class hash'
这是代码
template <class T>
class hash{
public:
size_t operator()(const T the_key) const;
};
/* a specialization with type string */
template<>
class hash<string>
{
public:
size_t operator()(const string the_key) const {
unsigned long hash_value = 0;
int length = (int) the_key.length();
for (int i=0; i<length; i++)
hash_value = 5 * hash_value + the_key.at(i);
return size_t(hash_value);
}
};
可能是什么问题?
答案 0 :(得分:4)
这应该工作,除了您的代码中可能有using namespace std
之外,这会导致您的hash
模板与标准模板冲突。删除它,问题应该消失。