2012-02-06 26 views
6

Oto kod, który powoduje błąd:nie spełniających funkcji członka do wywołania 'Erase'

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 
} 

a błąd pojawia się:

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

Any napiwki? Dzięki.

+0

Co jest potrzeba 'it2'? Co powiesz na '_table.erase (it ++)'? – iammilind

Odpowiedz

7

Oto podpis map::erase w C++ 98:

void erase(iterator position); 

Funkcja ta zajmuje iterator ale jesteś przechodzącą const_iterator. Dlatego kod się nie skompiluje.

How do I fix this?

w C++ 11 nie jest to nawet problem, więc nie ma potrzeby ustalania. Dzieje się tak dlatego, że w C++ 11 funkcja map::erase ma następującą sygnaturę, a zatem akceptuje numer const_iterator.

iterator erase(const_iterator position); 

Jeśli nie można korzystać z nowego standardu, musisz zmienić swoje zmienne do iterator zamiast.

+0

Dziękuję. Rozdzierałem sobie włosy. – ontherocks

+0

Jak przekazać iterator? Przykładowy kod, proszę? – JackKalish

+0

@JackKalish Er .... 'm.erase (it)'? –

2

Zobacz, co pan mówi:

Scot Meyers w Effective STL

Pozycja 26. Wolę iteracyjnej do const iterator, reverse_iterator i const_reverse_iterator. Chociaż kontenery obsługują cztery typy iteratorów, jeden z tych typów ma uprawnienia, których inni nie mają. Ten typ jest iteratorem, iterator jest wyjątkowy.

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 

Pozycja 27. Zastosowanie odległość i przejść do konwersji const_iterators pojemniku do iteratorów.

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 

Co działa to postęp i odległość

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) 
Powiązane problemy