473,566 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_emp ty ())
std::cout << Stack::pop () << "\n";
}

"
/tmp/ccTlYVih.o: In function `Stack::push(in t)':
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack4pushEi+0 x4): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack4pushEi+0 x10): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack4pushEi+0 x16): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack::pop()':
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack3popEv+0x 5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack3popEv+0x a): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack3popEv+0x 11): undefined
reference to `Stack::stack'
/tmp/ccTlYVih.o: In function `Stack::is_empt y()':
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack8is_empty Ev+0x5): undefined
reference to `Stack::top'
/tmp/ccTlYVih.o: In function `Stack::is_full ()':
/tmp/ccTlYVih.o(.gnu .linkonce.t._ZN 5Stack7is_fullE v+0x5): undefined
reference to `Stack::top'
collect2: ld returned 1 exit status
"

Jul 25 '05 #1
7 1758


sh*****@gmail.c om 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_emp ty ())
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.c om 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_emp ty ())
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.c om 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
2802
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 static accessor function that returns the variable's value. I reference this function in class B as ClassA::GetValueOfVariable(). Although this...
11
4583
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member...
4
2482
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
2413
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 variables, but don't know how to do it ? I still receive errors when I try to use it.
6
2533
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 called CUtils with a static method called print2std()
9
2952
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
14415
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 the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
22
2725
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 fun() {
12
3097
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 only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
2
1891
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); virtual ~CX();
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.