【发布时间】: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