簡體   English   中英

C ++ / g ++重載增量運算符

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

我正在嘗試實現一個雙鏈表,並希望創建一個迭代器。 結構是:

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

現在我想實現兩個運算符的重載:

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

現在有兩個錯誤:

  • 找不到會員聲明
  • 類型“迭代器”無法解析

當我使其他運算符(例如,解引用或(不等於)運算符)過載時,沒有錯誤。 錯誤只出現在g ++ - 編譯器中。 visual c ++的編譯器沒有顯示任何錯誤,並且在這里正常工作。

在成員函數的離線定義中,該函數的返回類型不在類范圍內,因為尚未看到類名。 因此,將您的定義更改為如下所示:

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

您需要在返回類型中限定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