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--;
}
}
}
}
|