Advertisement

PLEASE !!!static member?

Started by August 17, 2000 03:42 PM
6 comments, last by Grugnorr 24 years, 4 months ago
.-I NEED help!... In my DDraw wrapper I have CDirectScreen class and CDirectSurface class. As there will be MANY CDirectSurface objects I NEED some of its members(variables) to be static: That means that just ONE copy of that variable is used for every object of that class. .-I do: private: static _var; public: static void SetVar(TYPE var) {_var=var;}; static TYPE GetVar() {return _var;}; .- This way I get linking errors. I´ve tried to do the assign this way: CClass::_var=value; ,but I get the same error. .- PLEASE HELP!!! .-Thank you very much for your help... What the hells!
What the hells!
quote:
private:
static _var;


You forgot to put a type declaration in there . You''ll need something like int, char, float, etc.

----------------------------------------
Who is this General Failure and why is he trying to read my hard disk?
Advertisement

.- The Senshi : Of course In the real code var has a type, that was just pseudocode! I get a linking error, not a compiling error...

.-Please help!



What the hells!
What the hells!
What''s the exact linker error?


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Why don''t you post the real code, then, so we can take a look at it?

----------------------------------------
Who is this General Failure and why is he trying to read my hard disk?
You need to actually declare the static variables elsewhere in code, not just in the class. I don''t know why... but oh, well!

    class Dinglehopper{   static int fork;};int Dinglehopper::fork;    


At least, that''s what worked for me. There might be a different way.

Trigon

I like food.
I like food.
Advertisement
You have to declare them in code like this:

// Header file

class Foo
{
static int fooVar;
}

// Source file
int Foo::fooVar = 0; // or whatever you want it to start at




The reason for this is because the static variables are basically globals with class scope (I know that kinda doesn''t make sense ) but they need to be declared as actual variables somewhere, because they don''t belong to any actual structure/class object, just to the scope of the class.


.- Thank you men(or women...)!

.-That was it!. I knew that to initialize a static class variable I have to put CClass::Variable=INITIALIZING_VALUE...
But as I didn´t need to initialize it to 0/NULL I wasn´t doing it...

.-Sorry for the delay in answering, although at work I have Internet and I´m posting messages here these days, I couldn´t try this at the program...

.-Thanks again...

What the hells!
What the hells!

This topic is closed to new replies.

Advertisement