Pregunta

¡Lo siguiente no se compila, y por mi vida no puedo ver por qué!

#include <list>
using namespace std;

list<char> myList;
list<int>::iterator it;

it = myList.begin();

El error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty>::_Iterator<_Secure_validation>' (or there is no acceptable conversion)
¿Fue útil?

Solución

Esto sucede porque list<char> and list<int> son dos clases diferentes. Entonces sus iteradores son de diferentes tipos también.
Si observa el código de clase std :: list verá algo como:

typedef _Iterator<_SECURE_VALIDATION_DEFAULT> iterator;

o

typedef _Iterator<bla_bla_bla> iterator;

Eso significa que cada nueva clase de clase define un nuevo tipo. En otras palabras, cada lista define su propio tipo de iterador.

Cambie su código a esto:

list<char>::iterator it;

Otros consejos

Porque el tipo de iterador es diferente:

list<char> myList; // char
list<int>::iterator it; // int

Tenga en cuenta que el tipo de la lista o cualquier otro contenedor no es solo el parámetro type , sino también todos los demás parámetros de la plantilla. Por ejemplo:

list<char, MyAllocator> mylist;
list<char, YourAllocator> yourlist;
// typeof mylist != type of yourlist      (!!!)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top