2012-02-07 20 views
2

以下のようなテンプレートクラスのオーバーロードについてはどうすればよいですか?テンプレートクラスと演算子のオーバーロード

template <class T> 
const_iterator& List<T>::const_iterator::operator++() 
{ 
    current = current->next; 
    return *this; 
} 

template <class T> 
const_iterator List<T>::const_iterator::operator++(int) 
{ 
    const_iterator old = *this; 
    ++(*this); 
    return old; 
} 

私は以下のようなエラーが発生します:戻り値の型が指定された時点で

List.cpp:17: error: expected constructor, destructor, or type conversion before ‘&’ token 
List.cpp:23: error: expected constructor, destructor, or type conversion before ‘List’ 
List.cpp:30: error: expected constructor, destructor, or type conversion before ‘&’ token 
List.cpp:35: error: expected constructor, destructor, or type conversion before ‘List’ 
+2

'型名一覧 :: const_iteratorの&リスト :: const_iteratorの::演算子++()' –

答えて

3
template <class T> 
typename List<T>::const_iterator& List<T>::const_iterator::operator++() 

、あなたはList<T>のいわゆるレキシカルスコープ内ではありません。そして囲みのスコープにタイプconst_iteratorがないので、あなたはエラーを受け取ります(ただし、IMHOは少し良くなります)。

C++ 11は、末尾の戻り値の型であるかもしれないための別のオプション:

template<class T> 
auto List<T>::const_iterator::operator++() 
    -> const_iterator& 
{ 
    // ... 
} 

しかし、最高のアイデアは、ちょうどクラス自体にインラインでこれらの事を定義することです。あなたは、クラス外の定義の特別な理由はありますか?

+0

FWIW、GCC 4.6のすべて、4.7と打ち鳴らす3.1 "const_iteratorのはタイプではない" と文句を言います。 OPはGCC 4.5を使用しているようです。 –

+0

OSXが更新するGCCのバージョンを使用しています。 私が授業内に建築していないのは、これが宿題の一部であるからです。 – jordaninternets

+0

また、私はこれを答えとして受け入れるために待っている間に迅速な質問をします。どのようにして、ちょうどT型でこのようなものをどのようにフォーマットすればよいでしょうか? 'template T&List :: const_iterator :: retrieve() {//リスト内の対応する要素への参照を返します。 return current-> data; } ' – jordaninternets

関連する問題