ISO C++ forbids declaration of ‘iterator’ with no type

xcode下编译报错:ISO C++ forbids declaration of 'iterator' with no type

出错点:

template<class T>
class MyVector: public std::vector<T>
{
public:
	typedef std::vector<T> vecType;
	typedef vecType::iterator vecIterator; //报错

	MyVector(){};
	MyVector(const vecType& _vec);
	//.......
	//.......
};

根据别人的回答,http://stackoverflow.com/questions/1732895/error-iso-c-forbids-declaration-of-iterator-with-no-type。找到原因:

“The reason being being that iterator is dependent on what T is, so the compiler can't be sure it's a typename unless you tell it.”

加 typename,修改后编译通过:

template<class T>
class MyVector: public std::vector<T>
{
public:
	typedef std::vector<T> vecType;
	typedef typename vecType::iterator vecIterator; //typename

	MyVector(){};
	MyVector(const vecType& _vec);
	//.......
	//.......
};

mark! 还有一点“C++编译器不能支持对模板的分离式编译”,why? google.....

原文链接: https://www.cnblogs.com/fjut/archive/2013/03/08/2950634.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    ISO C++ forbids declaration of 'iterator' with no type

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/79993

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午7:21
下一篇 2023年2月9日 下午7:21

相关推荐