By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,465 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,465 IT Pros & Developers. It's quick & easy.

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

P: n/a
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+0x 5): 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
"

Jul 25 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


sh*****@gmail.com wrote:
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

Jul 25 '05 #2

P: n/a
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

Jul 25 '05 #3

P: n/a
sh*****@gmail.com wrote:
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

Jul 25 '05 #4

P: n/a
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.

Jul 25 '05 #5

P: n/a
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

Jul 25 '05 #6

P: n/a
I got it. Thanks Steve

Jul 25 '05 #7

P: n/a
shah...@gmail.com wrote:
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.

Jul 25 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.