Hey guys,
Well I ran this code a few days ago and it worked fine, I don't know what happened suddenly its giving me an error again. Well there are basically 3 parts to the code, so I\ll just put them up, its giving me an error saying:
g++ -o main main.cc
/tmp/cc3KIEz0.o(.text+0x143): In function `main':
: undefined reference to `Stack::Stack()'
/tmp/cc3KIEz0.o(.text+0x14c): In function `main':
: undefined reference to `Stack::getPos()'
/tmp/cc3KIEz0.o(.text+0x158): In function `main':
: undefined reference to `Stack::init()'
/tmp/cc3KIEz0.o(.text+0x1b7): In function `main':
: undefined reference to `Stack::push(double)'
/tmp/cc3KIEz0.o(.text+0x1c8): In function `main':
: undefined reference to `Stack::nitems()'
/tmp/cc3KIEz0.o(.text+0x21e): In function `main':
: undefined reference to `Stack::empty()'
/tmp/cc3KIEz0.o(.text+0x22b): In function `main':
: undefined reference to `Stack::pop()'
/tmp/cc3KIEz0.o(.text+0x281): In function `main':
: undefined reference to `Stack::~Stack()'
/tmp/cc3KIEz0.o(.text+0x29b): In function `main':
: undefined reference to `Stack::~Stack()'
collect2: ld returned 1 exit status
Which well would tell me that there probably is some naming problem somewhere but I can't find where it can be.
The code is like this:
#include <iostream>
#include "Stack.hh"
#include "main.cc"
using std::cout; //The removal of "using namespace std" was done in order to avoid the conflict
using std::endl; //with the global variable count
// const int
// Stack::LEN = 80 ; // default stack length
//Implementation
Stack::Stack(int buff) //Constructor
{
int pos = buff;
s = newdouble[pos];
cout << buff << endl;
init();
}
Stack::Stack()
{
pos = buff;
s = newdouble[pos];
init();
}
Stack::~Stack()
{
delete[] s;
} //Destructor
void
Stack::init()
{
s = newdouble[pos];
count = 0;
}
int
Stack::nitems()
{
return count;
}
bool
Stack::full()
{
return (count >= pos - 1); // Change is made here; shows that the stack is full and expands the stack only if the the user requires it
}
bool
Stack::empty()
{
return (count == 0);
}
void
Stack::push(double c)
{
if (full())
{
if (isFull)
{
cout << "Stack is full" << endl;
isFull = false;
}
if (count == pos)
{
cout << "Resizing Stack" << endl ;
grow(5);
isFull = true;
}
}
s[count++] = c ;
return; // This time has come after s[count] becuase otherwise nitems seems to return the incorrect number of elements
}
double
Stack::pop()
{
if (empty()) {
cout << "Stack::pop() Error: stack is empty" << endl ;
return 0 ;
}
return s[--count] ;
}
void
Stack::inspect()
{
for(int i = pos-1; i >= 0; i--)
cout << "Position: " << i << " Value: " << s[i] << endl;
}
int
Stack::getPos()
{
return pos;
}
void
Stack::grow(int delta)
{
int temp = pos;
pos += delta;
double *news = newdouble[pos];
for (int i =0; i < temp; i++)
{
news[i]= s[i];
}
delete[] s;
s = news;
}
Thanks so much, it definitely helped.
Though I did try compiling together using g++ -o main main.cc Stack.cc - it gives me an error when I do this, but if i compile them separately it works just fine.
Thanks a ton.
@taikodragon : I can't seem to remember why I added "main.cc", maybe to give it some kinda link perhaps. Anyways I will get rid of it now and see if it compiles well.
@Chrisname: Well basically the program compiles and runs well for the first time. Then if I compile it again, it gives me some random values and stuff. I don't really remember because I changed the way I compiled it since and haven't looked back!
If it does come about again, I\ll let you know.