【问题标题】:No matching function on hashtable哈希表上没有匹配函数
【发布时间】:2017-03-20 19:16:38
【问题描述】:

当我尝试从以下代码行调用创建哈希函数时,我收到错误 no matching function for call

 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 中你没有返回值,并且类声明不完整(缺少类名)
  • myHash的定义是什么?
  • checkTest的定义是什么?
  • HashTable myHash; bool checkTest(string testName, string whatItShouldBe, string whatItIs)

标签: c++ hashtable stdlist buckets


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2012-05-18
    • 2013-05-19
    • 2021-04-23
    • 2016-03-25
    • 2011-02-27
    相关资源
    最近更新 更多