9
如何將模板類傳遞給另一個類的構造函數?我試圖將模板化的哈希表類傳遞給菜單類,這將允許我允許用戶決定哈希表的類型。將模板類作爲參數傳遞
template <class T>
class OpenHash
{
private:
vector <T> hashTab;
vector <int> emptyCheck;
int hashF(string);
int hashF(int);
int hashF(double);
int hashF(float);
int hashF(char);
public:
OpenHash(int);
int getVectorCap();
int addRecord (T);
int sizeHash();
int find(T);
int printHash();
int deleteEntry(T);
};
template <class T>
OpenHash<T>::OpenHash(int vecSize)
{
hashTab.clear();
hashTab.resize(vecSize);
emptyCheck.resize(vecSize);
for (int i=0; i < emptyCheck.capacity(); i++)
{
emptyCheck.at(i) = 0;
}
}
所以我有這個類開放式哈希是模板化的,因爲它應該允許添加任何類型的,我有這個工作,如果啓動它的一個對象在我的主,改變輸入類型等
int main()
{
cout << "Please input the size of your HashTable" << endl;
int vecSize = 0;
cin >> vecSize;
cout << "Please select the type of you hash table integer, string, float, "
"double or char." << endl;
bool typeChosen = false;
string typeChoice;
cin >> typeChoice;
while (typeChosen == false)
{
if (typeChoice == "int" || "integer" || "i")
{
OpenHash<int> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "string" || "s")
{
OpenHash<string> newTable(vecSize);
hashMenu<OpenHash> menu(newTable);
typeChosen = true;
}
else if (typeChoice == "float" || "f")
{
OpenHash<float> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "double" || "d")
{
OpenHash<double> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "char" || "c" || "character")
{
OpenHash<char> newTable(vecSize);
typeChosen = true;
}
else
{
cout << "Incorrect type";
}
}
return 0;
}
在我的主要問題,我想問問用戶他們哪些類型的哈希表。取決於他們輸入的內容應該用他們想要的類型創建這個類的一個實例,然後將它傳遞給另一個稱爲菜單的類,它應該允許它們從哈希類中調用函數。
你想傳遞一個類模板或類模板特殊化?例如。 'std :: less'或'std :: less'? –
dyp
hashF是我的散列函數。我想通過一個模板化的類。 – Shaun1810