2016-07-16 3 views
0

Я получаю ошибкуОшибка компиляции при использовании std :: hash; не называет тип шаблона

"hashmap.hpp:63:14: error: ‘hash’ in namespace ‘std’ does not name a template type std::hash hash;"

Я не понимаю, почему я должен реализовать свою собственную специализацию, когда я передаю в типа T, который, вероятно, будет один из типов который работает для хэш-функции.

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; 
} 
+0

Что такое компилятор? Попробуйте скомпилировать его с опцией '-std = C++ 11'. – songyuanyao

+0

, который сделал трюк! Спасибо. если вы опубликуете его как обычное решение, я могу дать вам ответ. –

ответ

1

std::hash была введена с C++ 11, вам нужно C++ 11 поддержки для его использования.

Если вы работаете с GCC, вам необходимо добавить опцию компиляции -std=c++11.