0

I can't use the std container iterator in a template class.

I get this errors :
error C2061: syntax error : identifier 'iterator'
error C2238: unexpected token(s) preceding ';'

Here the source code:

#include <list>
#include <functional>
#include <memory>

template<typename TParam>
class MyClass
{
public:
    using TFunction = std::function<void(TParam)>;
    using TContainer = std::list<TFunction>;
    using TIterator = TContainer::iterator; // compilation error here!
};

How to use the iterator of the container in the templated class?

Antoine
  • 910
  • 1
  • 9
  • 26
  • 1
    The compiler error is common, and what I wanted to do is specific, so I didn't find the same question.. – Antoine Jan 08 '16 at 11:43

1 Answers1

1

You need the typename keyword

using TIterator = typename TContainer::iterator; 

Depending on what TContainer ends up being, the iterator could be a static member or a nested type. The compiler will assume it's not a type unless you explicitly specify that with the typename keyword. See here for more details.

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434