当我尝试使用我的迭代器类

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是我的名单和迭代器类的头文件,我不知道是什么souceannotations或crtdefs

有帮助吗?

解决方案

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

其他提示

litb已经回答完全你的问题。我认为值得强调的是,在努力使C ++更容易使用,在C ++委员会增加了新的语法功能的声明。其结果是,你可以定义你的函数如下(的 n2541 ),而无需额外的资格:

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

根据所支持的特征列表,GCC 4.4已经有这个功能。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top