简体   繁体   中英

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. The compiler of visual c++ doesn't show any errors and it works fine there.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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