Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2005, 08:25 PM
shahehe@gmail.com
Guest
 
Posts: n/a
Default C++ class with static member function ???????

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
"

  #2  
Old July 25th, 2005, 08:35 PM
Dan Cernat
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????



shahehe@gmail.com wrote:[color=blue]
> 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";
> }
>[/color]
You forgot to initialize the static members:

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

/dan

  #3  
Old July 25th, 2005, 08:45 PM
mrstephengross
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

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

  #4  
Old July 25th, 2005, 08:45 PM
Puppet_Sock
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

shahehe@gmail.com wrote:[color=blue]
> 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";
> }[/color]

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

  #5  
Old July 25th, 2005, 08:55 PM
shahehe@gmail.com
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

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.

  #6  
Old July 25th, 2005, 08:55 PM
mrstephengross
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

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

  #7  
Old July 25th, 2005, 09:05 PM
shahehe@gmail.com
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

I got it. Thanks Steve

  #8  
Old July 26th, 2005, 12:15 AM
Jaspreet
Guest
 
Posts: n/a
Default Re: C++ class with static member function ???????

shah...@gmail.com wrote:[color=blue]
> 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.[/color]

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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles