为什么这告诉我hash2不是模板?

Why is this telling me thehash2 is not a template?

本文关键字:告诉我 hash2 为什么      更新时间:2023-10-16

我很困惑,为什么终端告诉我,hash2不是一个模板,当我试图编译。

我疯了吗?

template<typename Symbol>
class thehash
{
public:
    size_t operator()(const Symbol & item)
    {
        static thehash2<string> hf;
        return hf(item.getData());
    }
};

template<typename string>
class thehash2<string>
{
public:
    size_t operator()(const string & key)
    {
        size_t hashVal = 0;
        for(char ch : key)
            hashVal = 37 * hashVal + ch;
        return hashVal;
    }
};
template<typename string>
class thehash2<string>
{

这里有一个错误。你可能想写:

template<typename T>
class thehash2
{

注意,正如在注释中所说的,你不应该在模板中使用string作为类型名。模板参数最常用的名称通常是单个字母——T, U,…这样它们就不会轻易与真正的类型名称混淆。

第二选择:

class thehash2 : public thehash<std::string>
{

(根据您的实际需要)