开发者

Why does G++ tell me "Stack" is not declared in this scope?

I created the following two C++ files:

Stack.cpp

#include<iostream>

using namespace std;

const int MaxStack = 10000;
const char EmptyFlag = '\0';

class Stack {

    char items[MaxStack];
    int top;
public:
    enum { FullStack = MaxStack, EmptyStack = -1 };
    enum { False = 0, True = 1};
    // methods
    void init();
    void push(char);
    char pop();
    int empty();
    int full();
    void dump_stack();
};

void Stack::init()
{
    top = EmptyStack;
}

void Stack::push(char c)
{
    if (full())
        return;

    items[++top] = c;
}

char Stack::pop()
{
    if (empty())
        return EmptyFlag;
    else
        return items[top--];
}

int Stack::full()
{
    if (top + 1 == FullStack)
    {
        cerr << "Stack f开发者_StackOverflowull at " << MaxStack << endl;
        return true;
    }
    else
        return false;
}

int Stack::empty()
{
    if (top == EmptyStack)
    {
        cerr << "Stack Empty" << endl;
        return True;
    }
    else
        return False;
}

void Stack::dump_stack()
{
    for (int i = top; i >= 0; i--)
    {
        cout << items[i] << endl;
    }
}

and StackTest.cpp

#include <iostream>

using namespace std;

int main()
{

Stack s;
s.init();

s.push('a');
s.push('b');
s.push('c');

cout << s.pop();
cout << s.pop();
cout << s.pop();

}

Then I try to compile with:

[USER@localhost cs3110]$ g++ StackTest.cpp Stack.cpp StackTest.cpp: In function int main()': StackTest.cpp:8: error:Stack' was not declared in this scope StackTest.cpp:8: error: expected ;' before "s" StackTest.cpp:9: error:s' was not declared in this scope

What am I doing wrong?


Your Stack is declared in Stack.cpp, as you said. You are trying to use it in StackTest.cpp. Your Stack is not declared in StackTest.cpp. You can't use it there. This is what the compiler is telling you.

You have to define classes in all translation units (.cpp files), in which you are planning to be using them. Moreover, you have to define them identically in all of these translation units. In order to satisfy that requirement, class definitions are usually separated into header files (.h files) and included (using #include) into each .cpp file where they are needed.

In your case you need to create header file Stack.h, containing the definition of Stack class (and constant definitions in this case) and nothing else

const int MaxStack = 10000; 
const char EmptyFlag = '\0'; 

class Stack { 

    char items[MaxStack]; 
    int top; 
public: 
    enum { FullStack = MaxStack, EmptyStack = -1 }; 
    enum { False = 0, True = 1}; 
    // methods 
    void init(); 
    void push(char); 
    char pop(); 
    int empty(); 
    int full(); 
    void dump_stack(); 
}; 

(Header files also benefint from use of so called include guards, but it will work as shown above for now).

This class definition should be moved from Stack.cpp to Stack.h. Instead you will include this .h file into Stack.cpp. Your Stack.cpp will begin as follows

#include<iostream>     

#include "Stack.h"

using namespace std;    

void Stack::init()      
{      
    top = EmptyStack;      
}      

// and so on...

The rest of your former Stack.cpp, i.e. member definitions, should remain as is.

The Stack.h should also be included into StackTest.cpp, in the same way, so your StackTest.cpp should begin as

#include <iostream>        

#include "Stack.h"

using namespace std; 

// and so on...

That's, basically, it. (And instead of providing an init method a better idea would be to create a constructor for the Stack class. But that's a different story).


You need to move the contents of the class Stack { ... code into a new file Stack.h. Then add the following line to the top of both StackTest.cpp and Stack.cpp

#include "Stack.h"


Because StackTest.cpp unaware of what Stack is.

You should extract Stack class definition into Stack.h file and then #include "Stack.h" into both Stack.cpp and StackTest.cpp.


or you could simply just rename it to Stack.h, though it is always best to have an .h file exposing only the class interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜