当我尝试使用我的迭代器类
时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是我的列表和迭代器类的头文件,我不知道什么是源代码或crtdef。
答案 0 :(得分:5)
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
}
注意typename
是必需的,因为list<T>::iterator
是一个以模板特化为前缀的类型,你需要告诉编译器 - 尽管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;
};
答案 1 :(得分:1)