Search code examples
c++visual-studio-2010assertvisual-studio-debugging

debug assertion failed! expression list iterators incompatible


I'm working on an assignment where I implement a Dictionary (ADT) in C++. I initially wrote the program in Xcode and everything seemed to work fine. However I recently found out that the grader uses Visual Studio 2010 so I tried to make sure my code would work ins VS2010 as well....

The problem I'm running into now is in my remove() function for the hash table. Basically, I am using a vector of lists and iterating through lists to delete a collision entry. However now when I try to test this remove() function I get the following error: debug assertion failed! expression list iterators incompatible. I tried looking through the Visual C++ documentation on asserts and everything looks like it should work.... is there something I'm overlooking?

Here's my code:

///**DELETE***////  
void remove(string key) {
    size_t index = hashString(key);
    list<Entry>& entry = table.at(index);
        for(typename list<Entry>::iterator it = entry.begin();
            it != entry.end();
            /**/)
        {
            if (it->data == key) {
                table[index].erase(it);
            } else
                it++;
        }
    //entry not found
    cout << "Error: Cannot Delete... No Such Entry"<< endl;
}

Solution

  • It's suspicious that you're ignoring the return value of erase. Does it work if you change the line containing the erase call to this?

    it = table[index].erase(it);
    

    Or indeed:

    it = entry.erase(it);
    

    See the documentation of list::erase here:

    http://www.cplusplus.com/reference/list/list/erase