我正在編寫一個程序來創建一個哈希表,同時給出選項以從表中插入和刪除一個值。我很快會添加一個選項來爲所有數據類型創建一個新表,需要爲散列表類使用模板。但是這個錯誤消息「錯誤:使用類模板'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哈希;' –