Typedef template

I am trying to add some typedef to my class, but the compiler reports syntactic erron in 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 following does not work either:

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

I searched for answers in many forums, but I cannot find a solution or workaround for this. Thanks for the help!

+4
source share
2 answers

You are missing typename :

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

There are many similar questions. For instance. take a look at the following:

+5
source

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

 typedef typename std::vector<T>::iterator iterator; ^ 
+2
source

Source: https://habr.com/ru/post/1440826/


All Articles