2013-03-23 70 views
2

我想實現一個雙鏈表,並且想要創建一個迭代器。其結構是:C++/g ++過載增量運算符

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++的編譯器不會顯示任何錯誤,它在那裏工作得很好。

回答

4

在成員函數的亂線定義,函數的返回類型是不上課的範圍,因爲類名尚未見過。因此,請將您的定義更改爲如下所示:

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

謝謝。這個問題花了很多時間,現在看到,我犯了什麼微不足道的錯誤...... – 2013-03-23 11:50:44

3

需要判定iterator在返回類型:

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