哈希表上没有匹配函数

No matching function on hashtable

本文关键字:函数 哈希表      更新时间:2023-10-16

当我尝试从以下代码行调用创建哈希函数时,我收到错误 没有匹配函数

 myHash.create("5", "Data");
 checkTest("testSimpleIntHash #1", "Data", myHash.retrieve("5"));

这个想法是,create 从 KeyValuePair 类接收密钥,然后该方法将对密钥进行哈希处理,转到该存储桶,然后插入键值对。我相信我收到此错误,因为我没有输出到字符串。但是,我无法弄清楚使用 create 方法将键值对正确输出为字符串的语法。(我正在使用std::list,并且我的KeyValuePair类运行正常(

template<typename T> class HashTable
 {
 public:
     static const unsigned int NUM_BUCKETS = 100000;
     HashTable create(const string& key, const T& item) 
     {
         int temp = hash(key);
         arr[NUM_BUCKETS].push_back(KeyValuePair<T>(temp, item));
     }
 private:
     int hash(const string& key) const;
     //Make an array of linked lists of KeyValuePair objects.
     list<KeyValuePair<T>> arr[NUM_BUCKETS];
 };
 template <typename T>
 int HashTable<T>::hash(const string& key) const {
     int temp = 0;
     for (int i = key.length(); i >= 0; i--) {
         temp = (13 * temp + key[i]) % NUM_BUCKETS;
     }
     return temp;
 }

create 方法中,可以返回对类本身的引用。

HashTable& create(const string& key, const T& item) 
 {
     int temp = hash(key);
     arr[NUM_BUCKETS].push_back(KeyValuePair<T>(temp, item));
     return *this;
 }

但无论如何,你没有签署它。

并且您需要在类HashTable中声明retrieve方法:例如:

string retrieve(const string& input) 
 {
     string result;
     //Not sure wath do you pretend to be this method.
     //But it will transform your input in your result
     return result;
 }