Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a template class with a private map member

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

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

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

However, this gets a compile error: identifier 'iterator'. I don't need to pass an abstract iterator since MyFunction knows that it is a map iterator (and will only be used as a interator for myMap) and will treat it as such (accessing and modifying myIter->second). Passing myIter->second to MyFunction is not enough since MyFunction will also need to be able to ++myIter;.

like image 644
MarkB42 Avatar asked Dec 28 '22 09:12

MarkB42


1 Answers

The compiler doesn't know that std::map<T,T>::iterator is a type—it could be anything depending on what std::map<T,T> is. You must specify this explicitly with typename std::map<T,T>::iterator.

like image 165
Philipp Avatar answered Jan 14 '23 02:01

Philipp