我正在尝试使用向量和类模板在C ++中创建一个打开的寻址哈希表。 使用测试器文件进行编译时,我遇到以下错误,我尝试了不同的方法,所有这些方法似乎都输出类似于以下内容的消息,在大多数错误之后,我似乎总是得到“注意:模板参数推导/替换失败:在test1.cpp包含的文件中……”。我不确定如何解决这个问题
当前的主要编译错误:
HashTable.h:116:20: error: ‘class HashNode<std::__cxx11::basic_string<char>,int>’ has no member named ‘getNext’; did you mean ‘getKey’?
while (current.getNext() != NULL)
~~~~~~~~^~~~~~~
getKey
HashTable.h:118:25: error: ‘class HashNode<std::__cxx11::basic_string<char>,int>’ has no member named ‘getNext’; did you mean ‘getKey’?
current = current.getNext();
~~~~~~~~^~~~~~~
getKey
HashTable.h:121:13: error: ‘class HashNode<std::__cxx11::basic_string<char>,int>’ has no member named ‘setNext’
current.setNext(newNode);
~~~~~~~~^~~~~~~
HashTable.h: In instantiation of ‘ValueType HashTable<KeyType,ValueType>::getvalue(KeyType) [with KeyType = std::__cxx11::basic_string<char>; ValueType = int]’:
test1.cpp:46:26: required from here
HashTable.h:88:5: error: no matching function for call to ‘begin(HashNode<std::__cxx11::basic_string<char>,int>&)’
for(HashNode<KeyType,ValueType> h : n)
#ifndef MY_HASH_TABLE
#define MY_HASH_TABLE
#include "HashNode.h"
#include <vector>
using namespace std;
template <class KeyType,class ValueType>
class HashTable {
typedef vector <HashNode<KeyType,ValueType> > Table;
Table *table; // size of table (# of buckets) is stored in the Table data structure
uint num; // number of entries stored in the HashTable;
template <class KeyType,class ValueType>
void HashTable<KeyType,ValueType>::insert(KeyType key,ValueType value)
{
uint index = hash_function(key);
HashNode<KeyType,ValueType> newNode;
newNode.assign(key,value);
if (table.at(index) != NULL)
{
HashNode<KeyType,ValueType> current;
current = table->at(index);
while (current.getNext() != NULL)
{
current = current.getNext();
}
current.setNext(newNode);
}
else
{
table->at(index) = newNode;
}
num++;
}
template <class KeyType,class ValueType>
int HashTable<KeyType,ValueType>::hash_function(KeyType key)
{
uint hash = key % size();
return hash;
}
template <class KeyType,ValueType>::size() {
return table->size();
}
template <class KeyType,class ValueType>
void HashTable<KeyType,ValueType>::erase(KeyType key)
{
uint hashValue = hash_function(key);
HashNode<KeyType,ValueType> l = table->at(hashValue);
for(auto it = l.begin(); it != l.end(); ++it){
if (it->getKey() == key) {
l.erase(it);
break;
}
}
num--;
}
template <class KeyType,class ValueType>
ValueType HashTable<KeyType,ValueType>::getvalue(KeyType key)
{
try
{
uint hashValue = hash_function(key);
HashNode<KeyType,ValueType> n = table->at(hashValue);
for(HashNode<KeyType,ValueType> h : n)
{
if(h.getKey() == key)
{
return h.getvalue();
}
}
throw KEY_NOT_FOUND;
}
catch(HashTableError)
{
cout<< "Error! KEY_NOT_FOUND";
}
return 0;
}
HashNode.h的代码
#ifndef MY_HASH_NODE
#define MY_HASH_NODE
using namespace std;
template <class KeyType,class ValueType>
class HashNode
{
KeyType key; // The hash node's key
ValueType value; // The key's associated datatemplate argument deduction/substitution failed
/* extend if necessary */
public:
HashNode(KeyType key,ValueType value)
{
this-> value = value;
this-> key = key;
} // constructor
HashNode(){}
KeyType getKey() { return key; }
ValueType getvalue() { return value; }
void assign(KeyType key,ValueType value);
// extend if necessary
};
template <class KeyType,class ValueType>
void HashNode<KeyType,ValueType>::assign(KeyType key,ValueType value)
{
this-> value = value;
this-> key = key;
}
/*
Implement the constructor,the assign method
and any methods that you may additionally need for the HashTable to work.
*/
#endif