2015-11-05 52 views
-2

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

帖子在帖子中完整的錯誤消息。 – NathanOliver

+0

它是一個模板類;當然,它需要一個模板參數! – anderas

+0

你爲你的HashTable類定義了一個模板,但你並沒有像這樣創建它。而不是'HashTable哈希;'將其創建爲'HashTable 哈希;' –

回答

1

你想散列什麼類型?決定。我們假設它是Y。然後,你需要用

HashTable<Y> hash;

更換

HashTable hash;

如果你想Y成爲std::string那麼你有一些工作要做。對於字符串類型,hashValue % 100將無意義。考慮使用std::hash而不是?然後把所有東西都拿出來用std::unordered_map

0

你需要指定模板參數T

HashTable<string> hash; 
     // ^^^^^^^^ 
相關問題