views:

161

answers:

2

The following does not compile, and I cannot for the life of me see why!

#include <list>
using namespace std;

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

it = myList.begin();

The 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)
+3  A: 

Because the type of the iterator is different:

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

Beware that the type of the list or any other container is not only the template type parameter, but all other template parameters as well. For example:

list<char, MyAllocator> mylist;
list<char, YourAllocator> yourlist;
// typeof mylist != type of yourlist      (!!!)
AraK
+5  A: 
AlexKR
Agg, I cannot believe I didnt see that :-S
Kragen
That usually means you need a short break ;)
AlexKR