2017-04-26 73 views
0

我有一个模板类看起来像这样:如何实现的std ::哈希模板类

template <int N, class TypeId> class Indexer { 
... 
} 

,我想在std::unordered_map使用它,我需要一个散列函数。 在我们已经有了类似(但上untemplated类)的东西基本代码,所以我试图做这样的:

namespace std { 
template <int N, class TypeId> 
struct hash<Indexer<N, TypeId> > { 
    size_t operator()(const Indexer<N, TypeId>& id) const noexcept { 
     ... 
    } 
}; 
} 

也颇为相似another answer。 不幸的是,这不起作用,只是给了一堆无用的错误。任何见解?

+4

“这不工作”是不是一个有用的问题说明。 –

+7

如果错误对您有帮助,您不需要问。但是他们对你无益的事实并不意味着他们对我们*毫无帮助,你希望解决这个问题。发布完整的错误消息,以及[mcve](重现它们的代码)。 – Angew

+1

错误信息的第一行不是以“包含在文件中”或“在实例化”或“从这里需要”开始的? – cpplearner

回答

2

看起来你在Indexer类定义的末尾缺少一个分号。

这工作:

#include <functional> 

template <int N, class TypeId> struct Indexer {}; 

namespace std { 
template <int N, class TypeId> 
struct hash<Indexer<N, TypeId> > { 
    size_t operator()(const Indexer<N, TypeId>& id) const noexcept { return 0; } 
}; 
} 

int main() { 
    return 0; 
} 
+0

分号在那里。该课程已经在其他地方使用过! – Bikush