2012-02-06 22 views
6

Aquí está el código que provoca el error:ninguna función miembro coincidente para la llamada a 'borrado'

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 
} 

Y el error que consigo:

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

Cualquier ¿consejos? Gracias.

+0

¿Cuál es la necesidad de 'it2'? ¿Qué tal '_table.erase (it ++)'? – iammilind

Respuesta

7

Aquí está la firma de map::erase en C++ 98:

void erase(iterator position); 

Esta función toma un iterator pero estás pasando un const_iterator. Es por eso que el código no compilará.

How do I fix this?

En C++ 11 esto ni siquiera es un problema, por lo que no necesita reparación. Eso es porque en C++ 11 la función map::erase tiene la siguiente firma, y ​​por lo tanto acepta un const_iterator.

iterator erase(const_iterator position); 

Si no puede utilizar la nueva norma, que tendrá que cambiar las variables a iterator lugar.

+0

Gracias. Me estaba rompiendo el pelo en esto. – ontherocks

+0

¿Cómo pasas un iterador? Código de ejemplo por favor? – JackKalish

+0

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

2

ver lo que el maestro dice:

Scot Meyers en STL eficaz

Artículo 26. Prefiero iterador iterador a const, reverse_iterator y const_reverse_iterator. Aunque los contenedores admiten cuatro tipos de iteradores, uno de esos tipos tiene privilegios que los demás no tienen. Ese tipo es un iterador, el iterador es especial.

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 

Artículo 27. Uso distancia y avance para convertir const_iterators de un contenedor a iteradores.

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 

lo que funciona es avance y la distancia

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) 
Cuestiones relacionadas