[英]Passing template class as parameter
How do I pass a templated class to the constructor of another class? 如何将模板化类传递给另一个类的构造函数? I am trying to pass a templated hash table class to a menu class which will allow me to then allow the user to decide the type of the hash table.
我试图将模板化的哈希表类传递给菜单类,这将允许我允许用户决定哈希表的类型。
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;
}
}
So I have this class Open hash that is templated, because it supposed to allow for any type to be added, I have this working if initiate a object of it in my main and change input types etc. 所以我有这个模板的Open哈希是模板化的,因为它应该允许添加任何类型,如果在我的main中启动它的对象并更改输入类型等我有这个工作。
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;
}
In my main I want to ask the user what type they which to make the hash table. 在我的主要内容中,我想询问用户他们使用哪种类型的哈希表。 depending what they enter it should create a instance of this class with the type they want and then pass this to another class called menu which should allow them to call functions from the hash class.
根据他们输入的内容,它应该创建一个具有所需类型的类的实例,然后将其传递给另一个名为menu的类,该类允许它们从哈希类中调用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.