2009-05-21 13 views
1

私は演算子のオーバーロードで戻り値の型としてこれはなぜタイプの仕事を返さないのですか? (C++)

template<class T> 
class list 
{ 
public: 
class iterator; 
}; 

template<class T> 
class list<T>::iterator 
{ 
//stuff 
}; 

私のイテレータクラスを使用しようとすると、

template<class T> 
class list<T>::iterator 
{ 
public: 
iterator& operator++(); 
protected: 
list* lstptr; 
}; 

template<class T> 
iterator& list<T>::iterator::operator++() 
{ 
(this->lstptr)->current = ((this->lstptr)->current)->next; 
return this; 
} 

私はこれらのエラーを取得:

s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C2143: syntax error : missing ';' before '&' 
s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C2065: 'T' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C2039: 'Yes' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C2065: 'Yes' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(236) : error C2039: 'No' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(236) : error C2065: 'No' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(237) : error C2039: 'Maybe' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(237) : error C2065: 'Maybe' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(240) : error C2039: 'NoAccess' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(240) : error C2065: 'NoAccess' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(241) : error C2039: 'Read' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(241) : error C2065: 'Read' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(242) : error C2039: 'Write' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(242) : error C2065: 'Write' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(243) : error C2039: 'ReadWrite' : is not a member of 'vc_attributes' 
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(243) : error C2065: 'ReadWrite' : undeclared identifier 
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(582) : error C2146: syntax error : missing ';' before identifier 'time_t' 
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : error C2143: syntax error : missing ';' before 'identifier' 
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : warning C4091: 'typedef ' : ignored on left of 'localeinfo_struct' when no variable is declared 
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : fatal error C1075: end of file found before the left brace '{' at 'c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(174)' was matched 

NB: container_def.hは私のリストクラスとイテレータクラスのヘッダファイルです。souceannotationsやcrtdefsが何であるか分かりません。

答えて

5

iteratorは、その時点ではまだ分かりません。

template<class T> 
typename list<T>::iterator& list<T>::iterator::operator++() { 
    (this->lstptr)->current = ((this->lstptr)->current)->next; 
    return *this; // *this here, since this is a pointer only 
} 

list<T>::iteratorは、テンプレートの特殊化を前に付け型であり、あなたがそのことについてコンパイラに指示する必要があるため typenameが、必要とされる注意事項 - 事実にもかかわらず:あなたはそれが list<T>クラスでだ、それを指示する必要がありますVisual C++はおそらくそれの前に typenameを入れないコードを受け入れます。 typenameを省略すると、コンパイラはそれが型ではないと想定し、同じエラーメッセージを生成する必要があります。

あなたは可能性がまっすぐクラスにコードを入れることで、自分自身その面倒安全:

template<class T> 
class list<T>::iterator 
{ 
public: 
iterator& operator++() { 
    (this->lstptr)->current = ((this->lstptr)->current)->next; 
    return *this; // *this here, since this is a pointer only 
} 
protected: 
    list* lstptr; 
}; 
+0

私が探していたのは間違いなく(私はそれが存在するかどうかはわかりませんでした)リスト :: iterator&を使ってみましたが、コンパイラはそれを型として認識していないと私に言いました。 –

+0

よかった。コンパイラは実際にはいくつかの場所でそれを必要としているようです。いくつかの投稿では、msvC++を使っている人は、通常必要とされる特定の場所に置く必要はありません:) –

1

litbは完全にansweredあなたの質問を持っています。私は、C++を使いやすくするために、C++委員会が関数の宣言のための新しい構文を追加したことを強調する価値があると思います。その結果、あなたは次のように余分な資格を必要とせずに(n2541を)あなたの関数を定義することができるでしょうということである。

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

サポート機能listによると、GCC 4.4は既にこの機能を持っています。

関連する問題