4

I'm trying to add some typedef to my class, but the compiler reports a syntax erron on the following code:

    template<class T>
    class MyClass{
        typedef std::vector<T> storageType; //this is fine
        typedef storageType::iterator iterator; //the error is here

but the next does not work too:

        typedef std::vector<T>::iterator iterator;

I was looking for the answers on many forum but i can't find a solution or workaround for this. Thank you for your help!

2 Answers2

6

You are missing a typename:

typedef typename std::vector<T>::iterator iterator;

There are a lot of similar question. E.g. take a look at the following:

Community
  • 1
  • 1
nosid
  • 48,932
  • 13
  • 112
  • 139
2

std::vector<T>::iterator is a dependent type so you need to add typename before it.

typedef typename std::vector<T>::iterator iterator;
        ^
Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37
  • Why doesn't he need it for the first one? – David G Oct 19 '12 at 16:07
  • `std::vector` isn't a dependent type. It's type can be determined without instantiating any templates. To determine the type of `std::vector::iterator` you have to instantiate `std::vector`. – Dirk Holsopple Oct 19 '12 at 16:09