Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no matching member function for call to 'erase'

Here's the code that causes the error:

Factory.h:

#include <string>
#include <map>

namespace BaseSubsystems
{
    template <class T>
    class CFactory
    {
    protected:
        typedef T (*FunctionPointer)();
        typedef std::pair<std::string,FunctionPointer> TStringFunctionPointerPair;
        typedef std::map<std::string,FunctionPointer> TFunctionPointerMap;
        TFunctionPointerMap _table;
    public:
        CFactory () {}
        virtual ~CFactory();
    }; // class CFactory

    template <class T> 
    inline CFactory<T>::~CFactory()
    {
        TFunctionPointerMap::const_iterator it = _table.begin();
        TFunctionPointerMap::const_iterator it2;

        while( it != _table.end() )
        {
            it2 = it;
            it++;
            _table.erase(it2);
        }

    } // ~CFactory
}

And the error I get:

error: no matching member function for call to 'erase' [3]
                         _table.erase(it2);
                         ~~~~~~~^~~~~

Any tips? Thanks.

like image 823
frarees Avatar asked Feb 06 '12 03:02

frarees


2 Answers

Here's the signature of map::erase in C++98:

void erase( iterator position );

This function takes an iterator but you're passing a const_iterator. That's why the code won't compile.

How do I fix this?

In C++11 this isn't even a problem, so it doesn't need fixing. That's because in C++11 the map::erase function has the following signature, and thus accepts a const_iterator.

iterator erase( const_iterator position );

If you can't use the new standard, you'll have to change your variables to iterator instead.

like image 100
R. Martinho Fernandes Avatar answered Sep 22 '22 11:09

R. Martinho Fernandes


You are passing a const_iterator to a method which expects a plain iterator.

See: http://www.cplusplus.com/reference/stl/map/erase/

like image 35
Kyle Lutz Avatar answered Sep 22 '22 11:09

Kyle Lutz