模板函数定义迭代器不识别的解决方法

来源:互联网 发布:我的淘宝是什么流量 编辑:程序博客网 时间:2024/06/10 09:13

模板函数如下:

template <typename T> //重载<<的模板函数ostream &operator<<(ostream &out, const list<T> &lst){    list<T>::const_iterator ref = lst.begin();    for( ; ref != lst.end(); ref++)        out << *ref;    return out;}

编译时报错如下:
error: need ‘typename’ before ‘list< T>::const_iterator’ because ‘list< T>’ is a dependent scope
原因:
在list< T>前面需要用typename限定一下,因为编译器不知道list< T>::const_iterator是代表一个类型还是list中的一个成员iterator。
解决方法:

list<T>::const_iterator ref = lst.begin();

改为

typename list<T>::const_iterator ref = lst.begin();
阅读全文
0 0
原创粉丝点击