473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
"

Jul 25 '05 #1
7 1739


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
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
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
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
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
I got it. Thanks Steve

Jul 25 '05 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nobody You Know | last post by:
I need a member variable in class A known within an instance of class B, even though B does not instantiate A. My solution was to make the member variable private static, and create a public...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
9
by: baumann | last post by:
hi all, to implement a singleton class, one has define a static function in the singleton class, class A{ public: static A* getInstance() { static A a; return &a;
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.