There is no corresponding member function to call 'erase'

Here is 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? Thank you

+6
source share
3 answers

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

 void erase( iterator position ); 

This function takes an iterator , but you pass a const_iterator . Therefore, the code will not compile.

How to fix it?

In C ++ 11, this is not even a problem, so it does not need to be fixed. This is because in C ++ 11, the map::erase has the following signature, and thus accepts const_iterator .

 iterator erase( const_iterator position ); 

If you cannot use the new standard, you will have to change your variables to iterator instead.

+7
source

You pass const_iterator to a method that expects a simple iterator.

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

+3
source

See what the master says:

Scott Myers in Effective STL

Point 26. Prefer iterator for iterator const, reverse_iterator and const_reverse_iterator. Although containers support four types of iterators, one of these types has privileges that others do not. This type is an iterator, an iterator is special.

 typedef deque<int> IntDeque; //STL container and typedef lntDeque::iterator Iter; // iterator types are easier typedef lntDeque::const_iterator ConstIter; // to work with if you // use some typedefs Iter i; ConstIter ci; … //make i and ci point into // the same container if (i == ci ) ... //compare an iterator // and a const_iterator 

Item 27. Use distance and forward to convert container constant constants to iterators.

 typedef deque<int> IntDeque; //convenience typedefs typedef lntDeque::iterator Iter; typedef lntDeque::const_iterator ConstIter; ConstIter ci; // ci is a const_iterator … Iter i(ci); // error! no implicit conversion from // const_iterator to iterator Iter i(const_cast<Iter>(ci)); // still an error! can't cast a // const_iterator to an iterator 

What works - advancement and distance

 typedef deque<int> IntDeque; //as before typedef IntDeque::iterator Iter; typedef IntDeque::const_iterator ConstIter; IntDeque d; ConstIter ci; … // make ci point into d Iter i(d.begin()); // initialize i to d.begin() Advance(i, distance(i, ci)) //move i up to where ci is // (but see below for why this must // be tweaked before it will compile) 
+2
source

Source: https://habr.com/ru/post/907735/


All Articles