2015-11-05 9 views
-2

Я пишу программу для создания хэш-таблицы, предоставляя возможность вставки и удаления значения из таблицы. Я скоро добавлю возможность создать новую таблицу для всех типов данных, необходимо использовать шаблон для класса хеш-таблицы. Но это сообщение об ошибке «error: использование шаблона класса« 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

Post сообщения полной ошибки в вашем посте. – NathanOliver

+0

Это класс шаблонов; конечно, ему нужен шаблонный аргумент! – anderas

+0

Вы определили свой класс HashTable шаблоном, но не создали его как таковой. вместо хэша HashTable, создайте его как «HashTable hash;' –

ответ

1

Какой тип вы хотите использовать? Решите об этом. Скажем, это Y. Тогда вам нужно заменить

HashTable hash;

с

HashTable<Y> hash;

Если вы хотите Y быть std::string то есть работа. hashValue % 100 будет бессмысленным для строкового типа. Вместо этого используйте вместо этого std::hash? Затем вставьте все это и используйте std::unordered_map.

0

Вам необходимо указать параметр шаблона T:

HashTable<string> hash; 
     // ^^^^^^^^ 
Смежные вопросы