C++ template typename iterator

Solution 1:

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

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>*>::iterator is either a static field or a type.

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 typename ahead of the declaration:

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

Solution 2:

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

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

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

Secondly, even with typename the

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

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

Solution 3:

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

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