# using statics

This topic is 5398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have this clas, that is a super class of a lot of other classes, and at anytime anyone uses this class or any of its sub classes Id liket o check to see if I have to do some init for the class to work, If igured i could use a static variable to save if ive inited yet or not, but it seems to not me liking to check this variable on the classes constructor (kinda the point, anyone uses this class or any of its sub classes in an api i want to init some other stuff from a matlab library once and only once) this is mostly for a convience to the user of the API...I guess its not legal to do the following: (judging by the error i get) Any ideas how i can over come this or work around this? thanks. (and is this legal im just doing it wrong?)

//gives error: LNK2001: unresolved external symbol "private: static int SpecialArray::inited" ( ?inited@SpecialArray@@0HA)
class SpecialArray {
static int inited;

SpecialArray()
{
if(inited==0)
{
DoSpecialInit();
inited =1;
}
}

~SpecialArray()
{
if(inited==1)
{
DoSpecialTerminate();
inited=0;
}
}
...
...
...

};



##### Share on other sites
You have to give inited a "presence". In the .h or .cpp file add the line:

SpecialArray::inited=0;

or something to that effect.

##### Share on other sites
class a {
static int b;
};

// IN A .CPP FILE FOR ALL STATICS
static int a::b = 0;

or something like that.

##### Share on other sites
Quote:
 Original post by CodejoyLNK2001: unresolved external symbol "private: static int SpecialArray::inited" ( ?inited@SpecialArray@@0HA)

That's because you are trying to use the variable without ever defining it. All you did was declare it. To define it, put this in one of your translation units.

int SpecialArray::inited = 0; // Or some other value or no explicit initialization

edit:

Quote:
 SpecialArray::inited=0;

Quote:
 // IN A .CPP FILE FOR ALL STATICSstatic int a::b = 0;

You don't write static in the definition, only in the declaration, and the type still has to be specified.

##### Share on other sites
Quote:
 Original post by CodejoyAny ideas how i can over come this or work around this?
Click on the error in the output window and press F1. Alternately, if you don't have MSDN installed locally, look it up online.

Initiative. It's such a rare thing.

##### Share on other sites
I like all the "or something to that effect"

I guess im not sure where to put the SpecialArray::inited = 0; in main.cpp? outside of my class declaration but in the same .h file or what.i tried all and each one gave a different error hmm..

Even doing it according to my newly aquired C++ reference book it still gave an error, i think cause their example the class and the main were in the same file, in mine SpecialArray.h is used across many .h and .cpp files.DANG.

##### Share on other sites
Quote:
 Original post by CodejoyI like all the "or something to that effect"I guess im not sure where to put the SpecialArray::inited = 0; in main.cpp? outside of my class declaration but in the same .h file or what.i tried all and each one gave a different error hmm..Even doing it according to my newly aquired C++ reference book it still gave an error, i think cause their example the class and the main were in the same file, in mine SpecialArray.h is used across many .h and .cpp files.DANG.

You need to define it in exactly one translation unit. The easiest way to do this is to put:
int SpecialArray::inited = 0;

in exactly one cpp file. Don't forget that wherever you put it the definition of your class has to be visible (so IE if you put it in a cpp file, make sure you #include the file that has the class definition).

##### Share on other sites
Tried to put it into my main.cpp in main() {
but got this error:

main.cpp
D:\proj\main.cpp(18) : error C2655: 'initialized' : definition or redeclaration illegal in current scope
d:\proj\specialarray.h(12) : see declaration of 'initialized'

hmmm so not sure why i get that, doing it as stated in my reference book and by you. I know i want the equivalent to what java calls a "Static block"

##### Share on other sites
You don't put it in main() {, you put it at global scope (the line before)

##### Share on other sites
Quote:
 Original post by CodejoyTried to put it into my main.cpp in main()

Don't put it in a function.

• 21
• 15
• 9
• 17
• 13