Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: 'hash' is not a class template

Tags:

c++

c++11

hash

#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. 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.
like image 878
Fabian Patzlaff Avatar asked Oct 24 '25 04:10

Fabian Patzlaff


2 Answers

For posterity, I got the same error message when I had forgotten to #include <functional>.

like image 86
Ghostkeeper Avatar answered Oct 26 '25 18:10

Ghostkeeper


You are specializing std::hash<> in the global namespace and this is ill-formed.

The specialization must be declared in the same namespace, std. See the example for 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
        ...
like image 25
Maxim Egorushkin Avatar answered Oct 26 '25 17:10

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!