I am receiving the error
"hashmap.hpp:63:14: error: ‘hash’ in namespace ‘std’ does not name a template type std::hash hash;"
I don't understand why i need to implement my own specialization when I'm passing in type T, which will probably be one of the types that works for the hash function.
hashmap.hpp
#ifndef __HASHMAP_HPP__
#define __HASHMAP_HPP__
#define INITIAL_CAPACITY 10
#include <iostream>
#include <functional>
// A simple node struct for your separate chaining
// You may change this if you want
template <class K,class V>
struct KVNode
{
K key;
V value;
KVNode<K,V>* next;
KVNode(KVNode<K,V>* theNext = NULL) :
next(theNext)
{
}
};
template <class K, class V>
class HashMap {
public:
// Default constructor: creates an empty hash map
HashMap();
// Constructor setting the default value for our map
HashMap(V value);
// Destructor: deallocates all memory associated
// with the hash map
~HashMap();
// size() returns the number of elements currently
// stored in the hash map.
unsigned int size() const;
// set() inserts the given key value pair into the
// hash map. If the key is already in the map, then
// the value for that key is overwritten.
void set(const K& key, const V& value);
// get() returns the value associated with the key.
// If the key is not in the hash map, the default
// value should be returned.
V get(const K& key) const;
private:
// You are allowed to add any private variables you
// need to implement the functionality of a hash map
// It is also okay to add addional public or private
// methods you need, but you may not change the
// already existing method signatures.
// You may find these private members useful, but you are
// not required to use them.
KVNode<K,V>** map;
// To use this hash map with your own key type,
// you may need to provide a specialization of std::hash.
//std::hash<K> hash; #this was previous declaration but caused error
std::hash<K> hash;
// This is the default value to return if a key is not
// in the hash map
V default_value;
};
template <class K, class V>
HashMap<K,V>::HashMap() : default_value()
{
KVNode<K,V>** map = new KVNode<K,V>*[INITIAL_CAPACITY];
for (int i = 0; i < INITIAL_CAPACITY; ++i)
map[i] = new KVNode<K,V>();
}
template <class K, class V>
HashMap<K,V>::HashMap(V value)
{
//TODO: Implement this method
}
template <class K, class V>
HashMap<K,V>::~HashMap()
{
//TODO: Implement this method
}
template <class K, class V>
unsigned int HashMap<K,V>::size() const
{
//TODO: Implement this method
}
template <class K, class V>
void HashMap<K,V>::set(const K& key, const V& value)
{
//TODO: Implement this method
}
template <class K, class V>
V HashMap<K,V>::get(const K& key) const
{
//TODO: Implement this method
}
#endif // __HASHMAP_HPP__
temp.cpp
#include "hashmap.hpp"
#include <iostream>
#include <string>
#include <functional>
using namespace std;
int main()
{
HashMap<string,int> hm = HashMap<string,int>();
return 0;
}
std::hash
was introduced from C++11, you need C++11 supporting for using it.
If you work with GCC, you need to add the compilation option -std=c++11
.