在模板类中定义迭代器时的STL编译错误 [英] STL compilation error when defining iterator within template class
本文介绍了在模板类中定义迭代器时的STL编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码显示错误:
错误:type'std :: list< T,std :: allocator< _Tp1> >'不是从类型'Foo
派生的
错误:expected';'before'iter'
#include <list>
template <class T> class Foo
{
public:
std::list<T>::iterator iter;
private:
std::list<T> elements;
};
为什么,这是正确的?
推荐答案
您需要 typename std :: list< T> :: iterator
。这是因为 list
取决于模板参数,因此编译器不能知道它的名称迭代器
(很好,从技术上它可以知道,但C ++标准不工作这种方式)。关键字 typename
告诉编译器以下是类型的名称。
You need typename std::list<T>::iterator
. This is because list
depends on the template parameter, so the compiler cannot know what exactly is the name iterator
within it going to be (well, technically it could know, but the C++ standard doesn't work that way). The keyword typename
tells the compiler that what follows is a name of a type.
这篇关于在模板类中定义迭代器时的STL编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文