template specifiers not specified in declaration of ‘template<class Key> class hash’

尝试写显示特化样例的时候,写了如下代码

#include <iostream>
#include <cstddef>

using namespace std;

#define __STL_TEMPLATE_NULL template<>

template<class Key>
class hash {
public:
    void operator()() {
        cout << "Hash<T>" << endl;
    }
};

//explicit specialization
__STL_TEMPLATE_NULL
class hash<char> {//template specifiers not specified in declaration of ‘template<class Key> class hash’
public:
    void operator()() {
        cout << "Hash<char" << endl;
    }
};

编译时 在红色注释处报错

然后找原因半天找不到

换种写法,出现以下错误

C++::EC - expected initializer before '<' token

根据别人的文章才知道有类名“hash“有混淆。

 解决方法:改一下名字,加个命名空间,或者去掉<cstddef>头文件应该就可以了

posted @ 2018-07-09 11:00  _离水的鱼  阅读(431)  评论(0编辑  收藏  举报