S
shahehe
The following code seems fine to me but when I tried to compile it
using
g++ test.C, I got lots of errors, why?
#include <iostream>
static const int MAX_STACK = 100;
class Stack
{
private:
static int stack[MAX_STACK];
static int top;
public:
static void push (int item) { stack[top++] = item; }
static int pop () { return stack[--top]; }
static int is_empty () { return top == 0; }
static int is_full () { return top >= MAX_STACK; }
};
main ()
{
for (srandom (time (0L)); !Stack::is_full (); Stack:
ush (random
()))
;
while (!Stack::is_empty ())
std::cout << Stack:
op () << "\n";
}
"
/tmp/ccTlYVih.o: In function `Stack:
ush(int)':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x4): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x10): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x16): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack:
op()':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0x5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0xa): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0x11): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o: In function `Stack::is_empty()':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack8is_emptyEv+0x5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack::is_full()':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack7is_fullEv+0x5): undefined
reference to `Stack::top'
collect2: ld returned 1 exit status
"
using
g++ test.C, I got lots of errors, why?
#include <iostream>
static const int MAX_STACK = 100;
class Stack
{
private:
static int stack[MAX_STACK];
static int top;
public:
static void push (int item) { stack[top++] = item; }
static int pop () { return stack[--top]; }
static int is_empty () { return top == 0; }
static int is_full () { return top >= MAX_STACK; }
};
main ()
{
for (srandom (time (0L)); !Stack::is_full (); Stack:
()))
;
while (!Stack::is_empty ())
std::cout << Stack:
}
"
/tmp/ccTlYVih.o: In function `Stack:
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x4): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x10): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack4pushEi+0x16): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack:
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0x5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0xa): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack3popEv+0x11): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o: In function `Stack::is_empty()':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack8is_emptyEv+0x5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack::is_full()':
/tmp/ccTlYVih.o(.gnu.linkonce.t._ZN5Stack7is_fullEv+0x5): undefined
reference to `Stack::top'
collect2: ld returned 1 exit status
"