Frage

Wenn ich versuche, meine Iterator-Klasse zu verwenden

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

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

als Rückgabetyp in einer Überladen von Operatoren,

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

Ich erhalte diese Fehler:

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 ist die Header-Datei für meine Liste und Iterator-Klassen, ich habe keine Ahnung, was souceannotations oder crtdefs ist

.
War es hilfreich?

Lösung

iterator ist noch nicht an diesem Punkt bekannt. Sie müssen ihm sagen, es ist in der list<T> Klasse:

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
}

Beachten Sie die typename erforderlich ist, da list<T>::iterator ein Typ mit einer Vorlage Spezialisierung voran ist, und Sie müssen den Compiler darüber sagen - trotz der Tatsache, dass Visual C ++ akzeptiert wahrscheinlich Code nicht typename bevor sie setzen. Das Weglassen des typename sollte der Compiler davon ausgehen, es ist keine Art und soll Art produzieren, die gleiche Fehlermeldung.

Sie könnten sicher selbst, dass Ärger, indem Sie den Code direkt in die Klasse setzen:

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

Andere Tipps

litb hat beantwortet Ihre Frage vollständig. Ich denke, es ist erwähnenswert, dass in dem Bemühen, Hervorhebung C zu machen ++ einfacher zu bedienen, hat das C ++ Ausschuss eine neue Syntax für die Deklaration von Funktionen hinzugefügt. Das Ergebnis ist, dass Sie in der Lage sein, Ihre Funktion wie folgt zu definieren ( n2541 ) ohne die zusätzliche Qualifikation zu benötigen:

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

Nach der unterstützten Funktion Liste , 4.4 GCC hat bereits diese Funktion.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top