1
template<class T>
class iVector
{
protected:
    int _size;
    T * _vector;

public:
    typedef T * iterator;//My definition of iterator
    iVector(int n);
    iterator begin();
    iterator end();

};
//constructor:
template<class T>
iVector<T>::iVector(int n) : _size(n)
{

}
template<class T>
iterator iVector<T>::begin()
{

}
template<class T>
iterator iVector<T>::end()
{

}

I don't know why VS2017 tells me that the "iterator" is not defined. And Dev- C++ tells me that "iterator" does not name a type. The question happens on :

iterator iVector<T>::begin();
iterator iVector<T>::end();

But I think I have defined it on :

typedef T * iterator;

Thank you!

SunStar
  • 85
  • 5

2 Answers2

4

You need to qualify the name with the class name when use it out of the class definition. e.g.

template<class T>
typename iVector<T>::iterator iVector<T>::begin()
^^^^^^^^^^^^^^^^^^^^^
songyuanyao
  • 147,421
  • 15
  • 261
  • 354
1

As alternative to "verbose"

template<class T>
typename iVector<T>::iterator
iVector<T>::begin()
{
    // ...
}

you may use

template<class T>
auto iVector<T>::bagin()
-> iterator
{
    // ...
}
Jarod42
  • 173,454
  • 13
  • 146
  • 250