Archived

This topic is now archived and is closed to further replies.

Prozak

Static (int) in a Class - Bad Problem!

Recommended Posts

Prozak    898
Ok, this is really bothering me, so I hope someone can pull me out of this one... I have a class I use to handle multithreading, I call it CThread. It works perfect, but... Well, last night I learned something called "static" keyword, which basicly allows to have a variable that is common among all instances of that class. I created a simple "static int Number;" so that I could know how many threads i have currently running... I also added "Number++;" to my constructor... The thing is my compiler gives out an error, like he cannot find the declaration or something: [--------------------------------------------------] Linking... ThreadSys.obj : error LNK2001: unresolved external symbol "public: static int CThread::Number" (?Number@CThread@@2HA) Debug/H401.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. [--------------------------------------------------] So, what I''m I doing wrong? I''ve tried everything... Thanx for tha help guys,

[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

Share this post


Link to post
Share on other sites
Krunk    122
When you put a static variable in a class, that doesn''t allocate any space for the data. What you need to do is put the line:

int CThread::Number=0;


In your cpp file along with the method definitions.

Share this post


Link to post
Share on other sites
Prozak    898
and that initializes the data? an allocates space for it?
ok...
What if I have a static structure, do i have to do:
structvar Class::Var.A=0;
structvar Class::Var.B=0;
structvar Class::Var.C=0;
etc... ?!?

maybe someone should direct me to a link with an
in-depth look at static operation, cause this is
going to take me a while to swalow, *glleee*




[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

Share this post


Link to post
Share on other sites
jenova    122
no.

structvar Class::Var = { 0, 1, 3 };

just like you initialize any other structure. there''s no difference except for the class identifier "Class::". that''s it. sorry, no sites for ya. try "www.google.com". works for me 99% of the time.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Prozak    898
wow...
What if inside a structure i have an array of 60.000 bytes?

Can''t I use a structure constructor or something?
I started coding in C++, not C, so i might have missed
something in between...

I only use constructors/ destructors for classes, and
although I''ve heard that structures can also have their own
constructors I still have to see it implemented...

I really hope someone can help me, cause i''m despered...

[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

Share this post


Link to post
Share on other sites
Krunk    122
Oh, I forgot the helpful link I gave last time I answered this question here

The C++ FAQ Section 10.9 (BTW, the C++ FAQ is well worth a read - there's an awful lot of good info in there)

Structures can have constructors just like classes - In C++ they are basically classes with public visibility by default. The syntax is exactly the same and I think you can construct a static struct member in the same way as a normal class:

// In myclass.h
class myClass
{
...
static myStruct bigLoadOfData;
static myStruct moreData;
}

// In myclass.cpp
myStruct myClass::bigLoadOfData;
myStruct myClass::moreData(20);

The first would use the default constructor, the second would require a constructor taking an int parameter. I haven't tried this out though so feel free to flame me if I'm wrong

[EDIT] Added the bit about struct constructors.

Edited by - Krunk on January 16, 2002 6:23:18 PM

Share this post


Link to post
Share on other sites
Kippesoep    892
You could initialise your structure in a separate function call or in the constructor. The static keyword can be of help again. Stuff like this:


CMyClass::CMyClass ()
{
static bool structInitialised = false;
if (!structInitialised)
{
structInitialised = true;
//Initialise your structure here
}
}


You could, of course, put this bool somewhere else, but it would probably end up being either a global, a part of the struct itself or a part of the class (all of which means a more cluttered design in my opinion).

Share this post


Link to post
Share on other sites