1

considering the example below:

template <typename T>
class Vector {
    T* data;
public:
    class Iterator {
        T* i;
    public:
        Iterator& operator++();
    };
};

if I want to implement the 'operator++' function, it makes sence I'ld write like this:

template <typename T>
Vector<T>::Iterator& Vector<T>::Iterator::operator++() {
    i++;
    return *this;
}

but then I'm getting those error lines:

error C2143: syntax error : missing ';' before '&'
error C2065: 'T' : undeclared identifier
error C2923: 'Vector' : 'T' is not a valid template type argument for parameter 'T'

Why does it happen? and what should I do to solve this?

Thanks a lot.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304

1 Answers1

1

The compiler doesn't know that the member Iterator of Vector is necessarily a type, so you need to tell it with the typename keyword:

template <typename T>
typename Vector<T>::Iterator& Vector<T>::Iterator::operator++() {
//here^
    i++;
    return *this;
}

See this question for more details about typename.

Community
  • 1
  • 1
TartanLlama
  • 63,752
  • 13
  • 157
  • 193