C++ 模板类型名迭代器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11275444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:00:13  来源:igfitidea点击:

C++ template typename iterator

c++templatesiteratortypename

提问by CodeKingPlusPlus

Consider the following header file:

考虑以下头文件:

template <typename T> struct tNode
{
    T Data;                      //the data contained within this node
    list<tNode<T>*> SubNodes;       //a list of tNodes pointers under this tNode

    tNode(const T& theData)
    //PRE:  theData is initialized
    //POST: this->data == theData and this->SubNodes have an initial capacity
    //      equal to INIT_CAPACITY, it is set to the head of SubNodes
    {
        this->Data = theData;
        SubNodes(INIT_CAPACITY);   //INIT_CAPACITY is 10
    }

};

Now consider a line of code from another file:

现在考虑来自另一个文件的一行代码:

list<tNode<T>*>::iterator it();  //iterate through the SubNodes

The compiler is giving me this error message: Tree.h:38:17: error: need ‘typename' before ‘std::list<tNode<T>*>::iterator' because ‘std::list<tNode<T>*>' is a dependent scope

编译器给了我这个错误信息: Tree.h:38:17: error: need ‘typename' before ‘std::list<tNode<T>*>::iterator' because ‘std::list<tNode<T>*>' is a dependent scope

I have no idea why the compiler is yelling at me for this.

我不知道为什么编译器会为此对我大喊大叫。

回答by akappa

In list<tNode<T>*>::iterator, you have a dependant name, that is, a name that depends on a template parameter.

在 中list<tNode<T>*>::iterator,您有一个依赖名称,即依赖于模板参数的名称。

As such, the compiler can't inspect list<tNode<T>*>(it doesn't have its definition at this point) and so it doesn't know whether list<tNode<T>*>::iteratoris either a static field or a type.

因此,编译器无法检查list<tNode<T>*>(此时它没有定义),因此它不知道list<tNode<T>*>::iterator是静态字段还是类型。

In such a situation, the compiler assumes that it is a field, so in your case it yields a syntax error. To solve the issue, just tell the compiler that it is a type by putting a typenameahead of the declaration:

在这种情况下,编译器假定它是一个字段,因此在您的情况下它会产生语法错误。要解决此问题,只需通过typename在声明前放置 a 来告诉编译器它是一种类型:

typename list<tNode<T>*>::iterator it

回答by AnT

Firstly, as other answers already noted, type names nested into dependent types need to be prepended with the typenamekeyword.

首先,正如其他答案已经指出的那样,嵌套在依赖类型中的类型名称需要在typename关键字之前加上。

That keyword is not needed when the template is fully specialized, meaning that list<tnode<int>*>::iteratordoes not need typename, but when the outer class still depends on template parameter T, typenamemust be present.

当模板完全特化时不需要那个关键字,意思是list<tnode<int>*>::iterator不需要typename,但是当外部类仍然依赖模板参数时Ttypename必须存在。

template <typename T> void foo() {
  list<tnode<int>*>::iterator it1; // OK without typename
  typename list<tnode<T>*>::iterator it2; // typename necessary
}

Secondly, even with typenamethe

其次,即使typename

typename list<tNode<T>*>::iterator it();

declaration will declare a function, not an iterator. Remove the ().

声明将声明一个函数,而不是一个迭代器。删除().

回答by mfontanini

list<tNode<T>*>::iteratoris a dependant name, a type that depends on a template parameter. In order to declare that variable, you need to use the typenamekeyword:

list<tNode<T>*>::iterator是一个依赖名称,一种依赖于模板参数的类型。为了声明该变量,您需要使用typename关键字:

typename list<tNode<T>*>::iterator it = ...;

回答by trueter

More backround on the answers above is provided here

此处提供了有关上述答案的更多背景信息

A Description of the C++ typename keyword

C++ typename 关键字的描述

I had a different but similar problem in that I wanted to typedef an iterator for child nodes with the :

我有一个不同但相似的问题,因为我想为子节点 typedef 一个迭代器:

typedef std::vector<NodeType*>::iterator ChildIterator;

which gave me the same compiler error. With the suggestions here and help of the above link, the solution to my problem is to use

这给了我同样的编译器错误。有了这里的建议和上面链接的帮助,我的问题的解决方案是使用

typedef typename std::vector<NodeType*>::iterator ChildIterator;

instead.

反而。