Can't figure out what happened!

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
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:

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include "Stack.hh"

using std::cout;
using std::endl;
using std::cin;

int main() {

     int buff;
         do 
    {
         cout << "Please specify size for memory allocation: "; 
         cin >> buff;
    }
    while (buff == 0);
      

  Stack s;
  buff = s.getPos();


  s.init() ; // initialize Stack
  
  // Write doubles into Stack
  int i ;
  for (i=0 ; i<2*buff ; i++) {
    cout << "pushing value " << i*i << " in stack" << endl ;
    s.push(i*i) ;
  }
  
  // Count doubles in stack
  cout << s.nitems() << " value in stack" << endl ;
  cout << "buff: " << buff << endl;
  
  // Read doubles back from stack
  while (!s.empty()) {
    double val = s.pop() ;
    cout << "popping value " << val << " from stack" << endl ;
  }

  return 0 ;
}


Stack.hh:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef STACK_HH
#define STACK_HH

class Stack 
{

private:

  // static const int LEN;
double *s;
 int count;
 //void init();
bool isFull;
 int pos;
 bool full();

public:
 int buff;
 void init();
Stack();
 Stack(int buff);
~Stack();
int nitems();
bool empty();
void push(double c);
double pop();
void inspect();
 int getPos();
 void grow(int delta);


};

#endif 


Stack.cc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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 = new double[pos];
  cout << buff << endl;
	init();
}  

Stack::Stack()
{
  pos = buff;
  s = new double[pos];
  init();
 } 
 
Stack::~Stack()
{ 
        delete[] s; 
	} //Destructor  

void
Stack::init() 
{ 
  s = new double[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 = new double[pos];
  for (int i =0; i < temp; i++)
    {
      news[i]= s[i];
    }
  delete[] s;
  s = news;

}




Any idea where I'm missing out something??
Thanks
You forgot to link the Stack.cc file.

In pieces:
(compile)
g++ -c main.cc 
g++ -c Stack.cc
(link)
g++ -o main main.o Stack.o

All at once:
g++ -o main main.cc Stack.cc

Hope this helps.
Hi Duoas,

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.
What's the error?
#include "main.cc"

Is there a reason you're including a .cc/cpp in another .cc/cpp?
Yeah, including source files usually doesn't end too well unless you're working with templates.
hey guys, this is new to me. what's the difference between #include "file.h" and #include "file.cc"?
You should not really include sources. There's an article that I think Disch wrote on the topic of includes; you can find it in the articles section.
hey thanks. i found the article.. i'll read it later, it's kinda long.
@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.

Thanks guys!
Topic archived. No new replies allowed.