ISO C++禁止声明“Stack”;没有类型
我下面有一个堆栈结构的头文件。我不明白的是这个错误困扰着我:
ISO C++ 禁止声明没有类型的“Stack”
这是代码:
#include <stdexcept>
class Element;
class Stack{
public:
Stack():first(0){}; //constructor
~Stack(); //destructor
void push(int d);
int pop()throw(length_error);
bool empty();
private:
Element *first;
Stack(const& Stack){}; //copy constructor
Stack& operator = (const& Stack){}; //assignment operator..
};
有人知道错误的含义吗?
I have below a header file for a stack structure. what I don't understand is this error it is jamming at me:
ISO C++ forbids declaration of 'Stack' with no type
Here's the code :
#include <stdexcept>
class Element;
class Stack{
public:
Stack():first(0){}; //constructor
~Stack(); //destructor
void push(int d);
int pop()throw(length_error);
bool empty();
private:
Element *first;
Stack(const& Stack){}; //copy constructor
Stack& operator = (const& Stack){}; //assignment operator..
};
does anyone have a clue what the error means?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
堆栈&运算符 = (const& Stack)
应该是Stack&运算符 = (const Stack&)
。您不能有指向引用或引用数组或任何内容的指针,因此编译器认为
&
必须结束声明的类型部分,并且以下Stack
必须是参数名称。但是const&
中没有类型,因此编译器表示您不能声明没有类型的参数Stack
。在旧版本的 C 中,有时会在可能出现类型但被省略的上下文中推断出类型int
,这就是为什么该错误谈到 ISO C++ 禁止这样做。Stack& operator = (const& Stack)
should beStack& operator = (const Stack&)
.You can't have a pointer to a reference or an array of references or anything so the compiler thinks that
&
must end the type part of the declaration and that the followingStack
must be the parameter name. However there's no type inconst&
so the compiler says that you can't declare the parameterStack
with no type. In old versions of C the typeint
was sometimes inferred in contexts where a type could appear but was omitted which is why the error talks about ISO C++ forbidding this.