STL list compiler error

Hey everyone, I have been implementing a Hash Table class made, and ran into a bit of a problem in my Delete function.

I have the hash table made up as
 
vector<list<HashNode<T,X>>> m_Table


My problem is when I iterate through the vector, and then the list in that current element to try and find the Node I want to delete, the STL list class gives me the error:

Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'HashNode<T,X>' (or there is no acceptable conversion)

Here's the Delete function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T, typename X>
void HashTable<T,X>::Delete(T key)
{
	HashNode<T,X> Node;
	HashNode<T,X> NodeTemp;
	list<HashNode<T,X>> temp;
	list<HashNode<T,X>>::iterator it;
	vector<list<HashNode<T,X>>>::iterator iter;

	for(iter = m_Table.begin(); iter != m_Table.end(); ++iter)
	{
		temp = *iter;

		for(it = temp.begin(); it != temp.end(); ++it)
		{
			Node = *it;
			if(key == Node.GetKey())
			{
				NodeTemp = Node;
				temp.remove(NodeTemp);  //compiler error here
			}
		}
	}
}


does anyone know why it's not letting me do the .Remove() function?

Any help is appreciated, Thanks..
There's two issues here:
(1) This won't actually delete the node from m_Table
(2) list::remove is not the function you want.

This code will only remove the node from temp, which is a copy of an element of m_Table. So, when something else calls this function, nothing will happen to the underlying data in your hashTable object.

2nd, what list::remove() does is it searches the list for an element which matches the value that it has been passed. What this code actually does is it tells the computer to search the list for the value which it has already found, and then delete it. What you want is list::erase(), which deletes the element at a certain position.

The compiler error was happening because, to see if it has found the element that needs deleting, list::remove() tests (element i'm searching for)==(element i'm currently testing). As (element i'm searching for) is of type HashNode<T,X>, the use of the == is undefined behaviour, so there was an error. (see http://www.cplusplus.com/doc/tutorial/classes2/ for more info)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <typename T, typename X>
void HashTable<T,X>::Delete(T key)
{
	list<HashNode<T,X>>::iterator it,temp;
	vector<list<HashNode<T,X>>>::iterator iter;

	for(iter = m_Table.begin(); iter != m_Table.end(); ++iter)
	{
		for(it = iter->begin(); it != iter->end(); ++it)
		{
			if(key == it->GetKey())
			{
                                temp=it;
                                it++;
				iter->erase(it);
                                it--;
			}
		}
	}
}
Last edited on
Thanks for you very informative response theranga!

it helped, Thanks.
no problems
Topic archived. No new replies allowed.