为什么这种返回类型不起作用? (C ++)

时间:2009-05-21 01:48:20

标签: c++ subclass return-type

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

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。

2 个答案:

答案 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)

litb完全有answered你的问题。我认为值得强调的是,为了使C ++更易于使用,C ++委员会为函数声明添加了一种新语法。结果是您将能够如下定义您的函数(n2541),而无需额外的资格:

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

根据支持的功能list,GCC 4.4已经具备此功能。