简体   繁体   English

有关模板和迭代器的错误

[英]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. 我不知道为什么VS2017告诉我未定义“迭代器”。 And Dev- C++ tells me that "iterator" does not name a type. 而且DevC ++告诉我,“迭代器”没有命名类型。 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! 谢谢!

You need to qualify the name with the class name when use it out of the class definition. 在类定义之外使用名称时,需要使用类名称来限定名称。 eg 例如

template<class T>
typename iVector<T>::iterator iVector<T>::begin()
^^^^^^^^^^^^^^^^^^^^^

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM