简体   繁体   English

ISO C ++禁止声明'Stack'没有类型

[英]ISO C++ forbids declaration of 'Stack" with no type

I have setup the following header file to create a Stack which uses an Array. 我已经设置了以下头文件来创建一个使用数组的Stack。 I get the following at line 7: 我在第7行得到以下内容:

error: ISO C++ forbids declaration of 'Stack" with no type. 错误:ISO C ++禁止声明没有类型的“堆栈”。

I thought the type was the input value. 我认为类型是输入值。 Appreciate your help. 感谢您的帮助。 Thank you. 谢谢。

#ifndef ARRAYSTACKER_H_INCLUDED
#define ARRAYSTACKER_H_INCLUDED
// ArrayStacker.h: header file
class ArrayStack {
    int MaxSize;
    int EmptyStack;
    int top;
    int* items;
public:
    Stacker(int sizeIn);
    ~Stacker();
    void push(int intIn);
    int pop();
    int peekIn();
    int empty();
    int full();
};
#endif // ARRAYSTACKER_H_INCLUDED

构造函数和析构函数具有类的名称,即ArrayStack ,而不是Stacker

The error: ISO C++ forbids declaration of " identifier " with no type. error: ISO C++ forbids declaration of " identifier " with no type. error indicates that the declared type of identifier or identifier , itself, is a type for which the declaration has not been found. error表示声明的标识符类型或标识符本身是尚未找到声明的类型。

For example, if you wrote the following in your code: 例如,如果您在代码中编写了以下内容:

ArrayStack Stack;

The line above would give you such an error if you had failed to include the header in which "ArrayStack" is defined. 如果您未能包含定义“ArrayStack”的标头,则上面的行会给您带来这样的错误。 You would also get such an error, if you accidentally used Stack instead of ArrayStack (eg when declaring a variable or when using it as function's return-type, etc.). 如果您不小心使用了Stack而不是ArrayStack (例如,在声明变量或将其用作函数的返回类型时等)时,您也会遇到这样的错误。 I should also point out that your header has a fairly obvious error that you probably want to correct; 我还应该指出,你的标题有一个相当明显的错误,你可能想要纠正; a class's constructor and destructor must match the name of the class. 类的构造函数和析构函数必须与类的名称匹配。 The compiler is going to be confused, because when it sees "Stacker", it is going to interpret it as a function named "Stacker" where you simply forgot to give it a return-type (it won't realize that you actually meant for that to be the constructor, and simply mispelled it). 编译器会变得困惑,因为当它看到“Stacker”时,它会把它解释为一个名为“Stacker”的函数,你只是忘了给它一个返回类型(它不会意识到你实际意味着因为它是构造函数,并且简单地误导了它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM