blueice
blueice

Reputation: 134

A bug about the template and iterator

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!

Upvotes: 1

Views: 53

Answers (2)

Jarod42
Jarod42

Reputation: 217245

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
{
    // ...
}

Upvotes: 1

songyuanyao
songyuanyao

Reputation: 172924

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()
^^^^^^^^^^^^^^^^^^^^^

Upvotes: 4

Related Questions