error: expected nested-name-specifier before 'const_iter' 的解答
来源:互联网 发布:unity3d导航系统 编辑:程序博客网 时间:2024/06/05 01:55
今天第一篇博文,以后就通过博客来多多积累学习经验了!
关于这种报错如今天遇到的 error: expected nested-name-specifier before 'const_iter' ,
friend ostream & operator <<(ostream & out,const Array<T>&aa){ out<<"["; typename const_iter it=aa.begin(); if(it!=aa.end()){ out<<*it; if(it!=aa.end()){ for(;it<aa.end();it++) out<<","<<*it; }这部分中编译就出错:
E:\iterm\A.h:56: error: expected nested-name-specifier before 'const_iter' typename const_iter it=aa.begin(); ^后来发现,关于模板中的typename 不能乱用,其目的在于表示相关的类类型的迭代器的返回值。
<pre name="code" class="html">template<typename T>class A_iterator{public: A_iterator(){ }//constructor A_iterator(typename list<T>::iterator it){ this->it = it; }//construtor with index typename list<T>::iterator getIt() const{ return it; }//function getIt T& operator*(){ return *it; }//overloading operator * typename list<T>::iterator& operator++(){ ++it; return it; }//overloading operator ++ typename list<T>::iterator operator++(int){ list<int>::iterator old = it; it++; return old; }//overloding operator ++ in back sequnce bool operator!=(const A_iterator& b){ return it!=b.getIt(); }//overloading operator != bool operator==(const A_iterator& b){ return it==b.getIt(); }//overloding operator ==private: typename list<T>::iterator it;};
类似这种形式是我在一个类中使用typename用来表示这种返回与类型相同的类型。
因为std::list<T>在实例化之前,编译器没办法判断iterator到底是成员还是类型,所以会出错。
在std::list<T>::iterator前加上typename关键字就行了。
0 0
- error: expected nested-name-specifier before 'const_iter' 的解答
- error: expected specifier-qualifier-list before ‘uint8_t’
- error: expected type-specifier before ‘PacServiceImpl’
- g++error:expected type-specifier before
- error: expected specifier-qualifier-list before xxx (a structure )
- 编译出现问题error: expected specifier-qualifier-list before ‘(’ token
- error: expected specifier-qualifier-list before xxx (a structure )
- error: incomplete type 'QFileDialog' used in nested name specifier
- error: expected template-name before '<' token
- error: expected class-name before '{' token
- error: expected class-name before '{' token
- error: expected class-name before ‘{’ token
- error: expected class-name before '{' token
- error: expected class-name before '{' token
- error: expected class-name before '{' token
- Expected specifier-qualifier-list before
- expected specifier-qualifier-list before
- expected specifier-qualifier-list before
- 适配器模式
- android开发环境搭建
- zoj 1919 poj 2337 Catenyms(欧拉路径求解)
- ch3. XML简介--操作Dom
- Java Decimal详解,附带大量实例
- error: expected nested-name-specifier before 'const_iter' 的解答
- cpu 大小端模式
- bzoj1198: [HNOI2006]军机调度
- 关于Apache Flex 4.12
- static 带来的奇怪的bug
- SeekBar(手拉进度条)应用
- Android上开源的酷炫的交互动画和视觉效果:Interactive-animation
- 最近面试各家公司用到的新技术
- 编写linux设备驱动程序