【问题标题】:Why is this telling me thehash2 is not a template? [closed]为什么这告诉我 hash2 不是模板? [关闭]
【发布时间】:2014-03-18 21:37:05
【问题描述】:

我很困惑为什么终端在我尝试编译时告诉我 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;
    }
};

【问题讨论】:

  • 简单语法错误;不要将&lt;string&gt; 放在class thehash2 之后。此外,我建议使用 string 以外的其他名称作为模板参数名称,因为它非常令人困惑;读者最初可能会认为您指的是std::string
  • 你需要定义或至少声明hash2before使用它。
  • template&lt;typename string&gt; class thehash2&lt;string&gt; 是完全错误的派生语法!!

标签: c++ hashtable


【解决方案1】:
template<typename string>
class thehash2<string>
{

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

template<typename T>
class thehash2
{

请注意,如 cmets 中所述,您不应在模板中使用 string 作为类型名。模板参数最常见的名称通常是单个字母 - TU、... 这样它们就不会轻易与真实类型名称混淆。

第二个选项:

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

(根据实际需要)

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2016-03-14
    • 2013-05-29
    相关资源
    最近更新 更多