简体   繁体   English

C ++ / g ++重载增量运算符

[英]C++/g++ overload increment operators

I'm trying to implement a double linked list and want to create an iterator. 我正在尝试实现一个双链表,并希望创建一个迭代器。 The structure is: 结构是:

template<class type>
class List {
    size_t listElementCnt;
    ...
public:
    ...
    class iterator {
        ...
    public:
        ...
        iterator& operator ++();
        iterator operator ++(int);
        ...
    };
    ...
 };

Now I want to implement the overload both operators: 现在我想实现两个运算符的重载:

template<class type>
typename iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename iterator List<type>::iterator::operator ++(int) {
    ...
}

Now there are two errors: 现在有两个错误:

  • Member declaration not found 找不到会员声明
  • Type "iterator" could not be resolved 类型“迭代器”无法解析

When I'm overload the other operators, such as the dereferencing or (in-)equals operator, there are no errors. 当我使其他运算符(例如,解引用或(不等于)运算符)过载时,没有错误。 The errors just appears with the g++-compiler. 错误只出现在g ++ - 编译器中。 The compiler of visual c++ doesn't show any errors and it works fine there. visual c ++的编译器没有显示任何错误,并且在这里正常工作。

In an out-of-line definition of a member function, the return type of the function is not in class scope, since the class name has not yet been seen. 在成员函数的离线定义中,该函数的返回类型不在类范围内,因为尚未看到类名。 So change your definitions to look like this: 因此,将您的定义更改为如下所示:

template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
    ...
}

You need to qualify iterator in the return type: 您需要在返回类型中限定iterator

template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
    ...
}

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

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