Map <T, T> :: iterator as parameter type

I have a template class with a private card member

template <typename T> class MyClass { public: MyClass(){} private: std::map<T,T> myMap; } 

I would like to create a private method that takes an iterator to the map

 void MyFunction(std::map<T,T>::iterator &myIter){....} 

However, this leads to a compilation error: the identifier is 'iterator'. I do not need to pass an abstract iterator, since MyFunction knows that it is a map iterator (and will only be used as interator for myMap) and will consider it as such (access and change myIter-> second). Passing myIter-> second to MyFunction is not enough, since MyFunction should also be able to ++ myIter ;.

+4
source share
3 answers

The compiler does not know that type std::map<T,T>::iterator - it can be anything, depending on what std::map<T,T> . You must specify this explicitly with typename std::map<T,T>::iterator .

+7
source

You should use typename :

 template <typename T> class MyClass { public: MyClass(){} private: void MyFunction(typename std::map<T,T>::iterator &myIter){....} std::map<T,T> myMap; } 
+2
source

You must add the keyword "typename" before std :: map <T, T> :: iterator in the parameter list.

+1
source

All Articles