Passing a template class to another class as an object parameter.

Hi I am working on a project using opened addressed hash tables. I have got the hash table part of my code to work fine. I have also gotten the template part to work.

However one of the features of the program is going to be the ability to allow the user to decide what type of input the hash table is going to take in. I want to allow the user to select a type in the main function and then depending on which type they choose pass an object of the hash table class with that type to another class as a parameter. This other class will be a menu class where users can then call functions from the hash table class in order to perform the functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  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;
		
		}
}


Above is the class and constructor of the hash table class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 interger, 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;
}


Above is the main method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

using namespace std; 

class hashMenu
{

	private:
	OpenHash<T> OpenHash;

	public:
	hashMenu (int);
	 
}

hashMenu::hashMenu (hashTable table)
{
	table = newTable;
}

Above is my attempt at the menu class which is currently inside a header file.

To clarify my main question is I'm just not really sure how I pass the hash table class over to the menu class.
The only way I can see this working is by simulating dynamic types with unions or boost.variant.
Last edited on
It's fine solved my problem.
Topic archived. No new replies allowed.