views:

32

answers:

3

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;.

+1  A: 

You should add "typename" keyword before std::map<T,T>::iterator in parameters list.

vnm
+5  A: 

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.

Philipp
+2  A: 

You must use typename:

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

    std::map<T,T> myMap;
}
jpalecek