Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have setup the following header file to create a Stack which uses an Array. I get the following at line 7:

error: ISO C++ forbids declaration of 'Stack" with no type.

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
like image 212
JDragon314159 Avatar asked Nov 29 '22 05:11

JDragon314159


2 Answers

The 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.

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. You would also get such an error, if you accidentally used Stack instead of ArrayStack (e.g. when declaring a variable or when using it as function's return-type, etc.). 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).

like image 38
Michael Aaron Safyan Avatar answered Dec 14 '22 19:12

Michael Aaron Safyan


The constructor and destructor have the name of the class, which is ArrayStack, not Stacker.

like image 56
GManNickG Avatar answered Dec 14 '22 20:12

GManNickG