C++ class with static member function ???????

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::push (random
()))
;

while (!Stack::is_empty ())
std::cout << Stack::pop () << "\n";
}



"
/tmp/ccTlYVih.o: In function `Stack::push(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::pop()':
/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
"
 
D

Dan Cernat

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::push (random
()))
;

while (!Stack::is_empty ())
std::cout << Stack::pop () << "\n";
}
You forgot to initialize the static members:

int Stack::top = 3;
int Stack::stack[] = {0};

/dan
 
M

mrstephengross

Try adding:

class Stack
{
...
};

int Stack::stack[MAX_STACK];
int Stack::top;

Also, change "static const int MAX_STACK = 100" to "const int
MAX_STACK=100".

--Steve
 
P

Puppet_Sock

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::push (random
()))
;

while (!Stack::is_empty ())
std::cout << Stack::pop () << "\n";
}

You have not yet actually arranged for any memory for the
variable top or the array stack. These have to be declared
outside the class. Someplace you need something like so.

int Stack::stack[MAX_STACK];
int Stack::top;

Socks
 
S

shahehe

Thanks. Here comes to my second question:

is there any way that I could initialize the static member within
main() function? as you know, if I put the above initialization
statements into main function, it does not work.
 
M

mrstephengross

Nope... The static instances have to be initialized outside of main.
You can always change them to whatever you want later on. Of course,
with the array, you're stuck. If you want the array to be of variable
size, use a vector.

--Steve
 
J

Jaspreet

Thanks. Here comes to my second question:

is there any way that I could initialize the static member within
main() function? as you know, if I put the above initialization
statements into main function, it does not work.

I dont think you could innitialise them inside main(). What you could
do is to initialise them in global space and then modify them as per
your requirements inside main().

I saw one of your members is an array. You may not be able to re-size
the array. If you dont intend on changing its size then just
innitialise it globally else go in for a data structre which lets you
modify its size say a vector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top