简体   繁体   English

错误:“哈希”不是类模板

[英]error: 'hash' is not a class template

#include <unordered_map>
#include <memory>
#include <vector>

template<> // Voxel has voxel.position which is a IVec2 containing 2 values, it also has a bool value
struct hash<Voxel> {  
size_t operator()(const Voxel & k) const  
    {  
        return Math::hashFunc(k.position);  
    }  
};  

template<typename T> // This was already given
inline size_t hashFunc(const Vector<T, 2>& _key)
{
    std::hash<T> hashfunc;
    size_t h = 0xbd73a0fb;
    h += hashfunc(_key[0]) * 0xf445f0a9;
    h += hashfunc(_key[1]) * 0x5c23b2e1;
    return h;
}

My main 我的主要

int main()
{
    Voxel t{ 16,0,true };
    std::hash(t);
}

Right now i am writing on an specialisation for std::hash. 现在我正在写std :: hash的专业化。 Now the online submission page always returns the following errors for my code. 现在,在线提交页面始终为我的代码返回以下错误。 I don't know why and what i did wrong. 我不知道为什么和做错了什么。

error: 'hash' is not a class template struct hash<>

and

error: no match for call to '(const std::hash<Math::Vector<int, 2ul> >)   (const Math::Vector<int, 2ul>&)' noexcept(declval<const_Hash((declval<const_Key&>()))>.

My own compiler only throws 我自己的编译器只会抛出

error: The argument list for "class template" std :: hash "" is missing.

为了后代,当我忘记使用#include <functional>时,会收到相同的错误消息。

You are specializing std::hash<> in the global namespace and this is ill-formed. 您正在全局命名空间std::hash<> ,这是格式错误的。

The specialization must be declared in the same namespace, std . 必须在相同的名称空间std声明特殊化。 See the example for std::hash : 请参阅std::hash的示例:

// custom specialization of std::hash can be injected in namespace std
namespace std
{
    template<> struct hash<S>
    {
        typedef S argument_type;
        typedef std::size_t result_type;
        result_type operator()(argument_type const& s) const
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM