Search code examples
c++dictionarydata-structureshashhashtable

Why is there a mistake in class specialization?


I'm trying to implement the dictionaries in C ++, but when I define the specialization of the class hash I get the error:

error: 'hash' is not a class template
error: template argument required for 'class hash'

Here is the code

template <class T>
class hash{
public:
  size_t operator()(const T the_key) const;
};


/* a specialization with type string */
template<>
class hash<string>
{
public:
  size_t operator()(const string the_key) const {
    unsigned long hash_value = 0;
    int length = (int) the_key.length();
    for (int i=0; i<length; i++)
      hash_value = 5 * hash_value + the_key.at(i);
    return size_t(hash_value);
  }
};

What could be the problem?


Solution

  • This should work, except that you probably had using namespace std in your code, which causes your hash template to conflict with the std one. Remove that, and the problem should go away.