C++ 将模板类作为参数传递
如何将模板类传递给另一个类的构造函数?我试图将一个模板化的哈希表类传递给一个菜单类,该菜单类允许我随后允许用户决定哈希表的类型C++ 将模板类作为参数传递,c++,class,templates,parameters,constructor,C++,Class,Templates,Parameters,Constructor,如何将模板类传递给另一个类的构造函数?我试图将一个模板化的哈希表类传递给一个菜单类,该菜单类允许我随后允许用户决定哈希表的类型 template <class T> class OpenHash { private: vector <T> hashTab; vector <int> emptyCheck; int hashF(string); int hashF(int); int hashF(double);
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;
}
}
模板
类OpenHash
{
私人:
向量哈希表;
向量空检查;
int-hashF(字符串);
int-hashF(int);
int hashF(双精度);
int hashF(float);
int-hashF(char);
公众:
OpenHash(int);
int getVectorCap();
int addRecord(T);
int-sizeHash();
int-find(T);
int printHash();
int deleteEntry(T);
};
模板
OpenHash::OpenHash(int vecSize)
{
hashTab.clear();
hashTab.resize(vecSize);
emptyCheck.resize(向量大小);
对于(int i=0;i
所以我有一个模板化的类openhash,因为它应该允许添加任何类型,如果在我的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;
}
int main()
{
cout向量大小;
字体选择;
while(typeselected==false)
{
if(typeChoice==“int”| |“integer”| |“i”)
{
OpenHash newTable(vecSize);
typeselected=true;
}
else if(typeChoice==“string”| |“s”)
{
OpenHash newTable(vecSize);
hashMenu菜单(newTable);
typeselected=true;
}
else if(typeChoice==“float”| |“f”)
{
OpenHash newTable(vecSize);
typeselected=true;
}
else if(typeChoice==“double”| |“d”)
{
OpenHash newTable(vecSize);
typeselected=true;
}
else if(typeChoice==“char”| |“c”| |“character”)
{
OpenHash newTable(vecSize);
typeselected=true;
}
其他的
{
cout您可以使用:
class Ctor {
public:
Ctor(const Other<int>&); // if you know the specific type
};
class{
公众:
Ctor(const Other&);//如果您知道具体的类型
};
或:
class{
公众:
模板
Ctor(const Other&);//如果您不知道具体的类型
};
要传递类模板还是类模板专用化?例如std::less
或std::less
?hashF是我的哈希函数。我要传递模板化的类。
class Ctor {
public:
template<class T>
Ctor(const Other<T>&); // if you don't know the specific type
};