Mark Macdonald
Mark Macdonald

Reputation: 43

C++ no matching function for list::erase

I am currently trying to cycle through a linked list of type char. When an argument is met (if it holds the letter 'w') I try to delete the list item using list::erase. I was just hoping to receive some advice towards using this function, I have only seen examples with type int linked lists, and I cannot tell if it won't work because it is char or what reason. When I try to compile the following code:

void filterFile(list <char> &myList)
{
    list<char>::const_iterator itr;

    for (itr = myList.begin(); itr != myList.end(); itr++ )
    { 
        if (*itr == 'w' || *itr == 'W'){
            itr = myList.erase (itr);
        }
    }
}

I receive the compiling error:

error: no matching function for call to ‘std::__cxx11::list::erase(std::__cxx11::list::const_iterator&)’ itr = myList.erase (itr);

All of what I've done so far has been based off what I have found here http://www.cplusplus.com/reference/list/list/erase/.

I also read about a node method to delete items in a list. I am just hoping to get this method working as it seems better and I am curious to learn what I've done wrong.

Upvotes: 2

Views: 1424

Answers (2)

This is a bug in GCC or more specifically libstdc++ that wasn't fixed until 4.9.0

The missing overload mandated by C++11:

iterator erase( const_iterator pos );

causes your code to fail.

Upvotes: 4

Tom Moers
Tom Moers

Reputation: 1261

Are you using C++11? std::list::erase only supports const_iterators since c++11: http://en.cppreference.com/w/cpp/container/list/erase

Upvotes: 5

Related Questions