Problems overloading index operator

So to sum it all up, I'm a bit stuck. I'm making a bucket hash style HashTable class that is an array of pointers to single linked nodes. I'm a bit perplexed as to how I should retrieve the values of the nodes from the array.
When I try to retrieve values using an index I get that Value, Key, and Next are not members of HashTable. I am not sure how to structure the index operator. By declaring it as a HashTable type function, doesn't that also declare the return type? How should I go about just returning the Node, not a HashTable object?
Any insight would be greatly appreciated, I feel as though I may be overlooking some small details. Thanks!

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
48
49
50
51
52
template<typename DATA_TYPE>
class HashTable
{
public:
	//DEFAULT && INIT CONSTRUCTOR
	HashTable(int size = DEFAULT_SIZE, int load = 0)
	{
		Size = size;
		Load = load;
		Data = new Node<DATA_TYPE>[Size];
	}

	//DESTRUCTOR
	~HashTable()
	{
		delete[] Data;
	}

	// INDEX OPERATOR
	HashTable& operator[] (int index) const
	{
		if( (index < 0) || (index >= Size) )
			throw; //create exception to throw

		return Data;
	}

// ...

	// encapsulated array to be filled with pointers to linked list nodes
	Node<DATA_TYPE>* Data;
	int Size, Load;

};

//Node struct in separate, included header

// Nodes for linked list
template<typename DATA_TYPE> 
struct Node
{
	Node(DATA_TYPE value = NULL, string key = DEFAULT_KEY, Node* next = NULL)
	{
		Value = value;
		Key=key;
		Next = next;
	}

	DATA_TYPE Value;
	string Key;
	Node* Next;
};
Last edited on
You tells compiler, you will return HashTable& and you are returning Node<DATA_TYPE>*
That's what I had figured, but when I change the function type to Node& it will not work as an operator for HashTable. Is there any way around this?

EDIT:
I see, I forgot to pass the <DATA_TYPE> through when I declared the index operator, so seeing as there was no member strictly "Node" it wouldn't compile. Thanks!
Last edited on
Node* HashTable::operator[] (int index) isn't works?
Topic archived. No new replies allowed.