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 full 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?