我正在编写一个程序来创建一个哈希表,同时给出选项以从表中插入和删除一个值。我很快会添加一个选项来为所有数据类型创建一个新表,需要为散列表类使用模板。但是这个错误消息“错误:使用类模板'HashTable'需要模板参数”不断出现,任何人有任何想法为什么?谢谢。错误:使用类模板'HashTable'需要模板参数
#include <vector>
#include <iostream>
#include <string>
using namespace std;
// ------------ Hash table ------------
template <class T>
class HashTable{
private:
vector<T> arrayofbuckets[100];
public:
void insertelement(string input);
void deleteelement(string remove);
}; // end of class
// ------------ MAIN ------------
int main()
{
HashTable hash;
// Creating the menu
char selection;
string Element;
string ElementDelete;
do{
cout << "--------------- Menu ---------------";
cout << "\n Press i to insert an element into the hash table";
cout << "\n Press d to delete an element from the hash table";
// Read the input
cin >> selection;
switch(selection)
{
// Inserting an element
case 'I':
case 'i':
{
cout << " Which element would you like to insert?: ";
cin >> Element;
hash.insertelement(Element);
}
break;
// Delete an element
case 'D':
case 'd':
{
cout << " Which element would you like to delete?: ";
cin >> ElementDelete;
hash.deleteelement(ElementDelete);
}
break;
// Exit the program
case 'e': {cout << "Goodbye! :D";}
break;
// Display message if input is not I, D, L, S, P or E
default : cout << "\n Invalid selection";
}
cout<<"\n";
} while(selection != 'e');
return 0;
} // End of main
// ------------ Functions for chained hash tables ------------
// Inserting an element
template <class T>
void HashTable<T>::insertelement(string input){
T hashValue = 0;
for(int i = 0; i<input.length(); i++){
hashValue = hashValue + int(input[i]);
}
hashValue = hashValue % 100; // HASH FUNCTION
arrayofbuckets[hashValue].push_back(input);
cout << " The element " << input << " has been put into value " << hashValue << endl;
} // End of insert function
// Deleting an element
template <class T>
void HashTable<T>::deleteelement(string remove){
T hashValue = 0;
for(int i = 0; i < remove.length(); i++){
hashValue = hashValue + int(remove[i]);
}
hashValue = hashValue % 100; // HASH FUNCTION
for (unsigned int i=0; i<arrayofbuckets[hashValue].size();){
if (arrayofbuckets[hashValue].at(i)==remove){
arrayofbuckets[hashValue].erase(arrayofbuckets[hashValue].begin()+i);
cout << " The element " << remove << " has been deleted from bucket " << hashValue << endl;
} else {
i++;
}
}
} // End of delete function
帖子在帖子中完整的错误消息。 – NathanOliver
它是一个模板类;当然,它需要一个模板参数! – anderas
你为你的HashTable类定义了一个模板,但你并没有像这样创建它。而不是'HashTable哈希;'将其创建为'HashTable哈希;' –