以下不编译,我不能为我的生活看到原因!
#include <list>
using namespace std;
list<char> myList;
list<int>::iterator it;
it = myList.begin();
错误:
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)
答案 0 :(得分:5)
这是因为list<char> and list<int>
是两个不同的类。
所以他们的迭代器也是不同的类型
如果你看一下std :: list类代码,你会看到类似的东西:
typedef _Iterator<_SECURE_VALIDATION_DEFAULT> iterator;
或
typedef _Iterator<bla_bla_bla> iterator;
这意味着新类型由每个不同的类列表定义。换句话说,每个列表定义自己的迭代器类型。
将您的代码更改为:
list<char>::iterator it;
答案 1 :(得分:3)
因为迭代器的类型不同:
list<char> myList; // char
list<int>::iterator it; // int
请注意,列表或任何其他容器的类型不仅是模板类型参数,还包括所有其他模板参数。例如:
list<char, MyAllocator> mylist;
list<char, YourAllocator> yourlist;
// typeof mylist != type of yourlist (!!!)