views:

724

answers:

2

When I try to use my iterator class

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

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

as a return type in an operator overloading,

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

I get these errors:

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 is the header file for my list and iterator classes, I have no idea what souceannotations or crtdefs are.

+5  A: 

iterator is not yet known at that point. You need to tell it it's in the list<T> class:

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
}

Notice the typename is required, since list<T>::iterator is a type prefixed with a template specialization, and you need to tell the compiler about that - in spite of the fact that Visual C++ probably will accept code not putting typename before it. Omitting the typename, the compiler should assume it's not a type and should sort of produce the same error message.

You could safe yourself that hassle by putting the code straight into the class:

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;
};
Johannes Schaub - litb
Keand64
Oh nice. Seems like the compiler does require it at some places actually. In some posts i've seen people using msvc++ are not required to put it at certain places where it is needed normally :)
Johannes Schaub - litb
+1  A: 

litb has answered your question completely. I think it worth highlighting that in an effort to make C++ easier to use, the C++ Committee has added a new syntax for the declaration of functions. The result is that you'll be able to define your function as follows (n2541) without needing the extra qualification:

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

According to the supported feature list, GCC 4.4 already has this feature.

Richard Corden