Working on a program where we must build a class for a stack and use it to check if strings are palindromes. Compiler is complaining about an undefined reference to the member functions in class Stack. I am not sure what I am doing wrong, as I believe I am using the exact same syntax as in the past. The files for the class are Stack02.h, Stack02.cpp, and p02.cpp is the main file for the program.
From Stack02.h
class Stack
{
int size;
int tos;
char* S;
public:
Stack(int sz=100); //Constructor
~Stack(); //Deconstructor
bool isFull(void);
bool isEmpty(void);
void Push(char v);
char Pop(void);
};
From Stack02.cpp:
#include "Stack02.h"
using namespace std;
//Exception handling for stacks
struct StackException
{
StackException{char* m}
{
cout << endl << "I am the Stack and I am " << m << "." << endl;
}
};
Stack::Stack(int sz):size(sz),tos(-1){S= new char[size]}; //Constructor
Stack::~Stack(){if(S) delete[] S;} //Deconstructor
bool Stack::isFull(void)
{
return tos >= size - 1;
} //Is the stack full?
bool Stack::isEmpty(void)
{
return tos < 0;
}
void Stack::Push(char v)
{
if(isFull) throw StackException("full");
S[++tos] = c;
}
char Stack::Pop(void)
{
if(isEmpty) throw StackException("empty");
return S[tos--];
}
The function the compiler is complaining about from p02.cpp:
bool IsPalindrome(string& c)
{
Stack s;
for (int a = 0; a < c.length(); a++) s.Push(c[a]);
for (int a = 0; !s.isEmpty() ;a < c.length())
{
if (c[a] !=s.Pop()) return false;
}
//can use default return value to check how output looks.
}
The makefile:
#------------------------------------------------
#Object files
#------------------------------------------------
obj = p02.o Stack02.o
#------------------------------------------------
#Link object files into executable file p02
#------------------------------------------------
p02: ${obj}
g++ -o p02 ${obj} -lm
#------------------------------------------------
#Compile p02.cpp that exercises class Stack
#------------------------------------------------
p02.o p02.cpp Stack02.h
g++ -g -c p02.cpp
#------------------------------------------------
#Compile Stack02.cpp that implements class Stack
#------------------------------------------------
Stack02.o Stack02.cpp Stack02.h
g++ -g -c Stack02.cpp
The compiler's complaints:
tt054@cs:~$ make p02
g++ p02.cpp -o p02
/tmp/ccYDuT0W.o: In function `IsPalindrome(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
p02.cpp:(.text+0x1c): undefined reference to `Stack::Stack(int)'
p02.cpp:(.text+0x4e): undefined reference to `Stack::Push(char)'
p02.cpp:(.text+0x9b): undefined reference to `Stack::Pop()'
p02.cpp:(.text+0xc9): undefined reference to `Stack::isEmpty()'
p02.cpp:(.text+0xe1): undefined reference to `Stack::~Stack()'
p02.cpp:(.text+0xfc): undefined reference to `Stack::~Stack()'
collect2: ld returned 1 exit status
make: *** [p02] Error 1