質問

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

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

注意:Container_def.h はリストおよびイテレータ クラスのヘッダー ファイルですが、sourceannotations や crtdef が何なのかはわかりません。

役に立ちましたか?

解決

iteratorはまだその時点では知られていません。あなたはそれがlist<T>クラスでだ、それを伝える必要があります:

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
}
Visual C ++は、おそらくその前にtypenameを入れていないコードを受け入れるという事実にもかかわらず - list<T>::iteratorは、テンプレートの特殊化を前に付け型であり、あなたはそのことについてコンパイラに指示する必要があるため、

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;
};

他のヒント

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

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

サポートされている機能に応じて リスト, GCC 4.4 にはすでにこの機能があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top