Sign in to follow this  

Initializing static class members.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's say I have a class where I absolutely need both the declaration and definition to be in the .h file. I can not split them up into .h and .cpp files. The problem I'm having is that I have a static member in that class that I need to initialize, the member is not a integral type (ie int, char, bool... ...), so I can not initialize it from within the class body. But when I initialize it outside of the class body, I get a really annoying LINKER error: fatal error LKN2005: "private: static class boost::basic_regex TestClass::attributePattern" already defined in a previous module fatal error LNK1169: one or more multiply defined symbols found How can I go about keeping the declaration and definition of a class all within a single .h file, initialize the static member on the class within the .h file, and avoid this linker error? Using "#pragma once" isn't helping either.

Share this post


Link to post
Share on other sites
you can initialize a static member in a header, but you run the risk of getting this problem...

why is it that you cannot initialize it in the cpp? That would be the easiest way to solve your problem...

Share this post


Link to post
Share on other sites

One thing you can try is instead of having attributePattern as a static member of TestClass, create a function that returns the static data in question.



Example:

basic_regex* TestClass::getAttribPattern

{

// Call the constructor here.
static basic_regex attributePattern();



// Return a pointer to it

return &attributePattern;
}



This way, attributePattern will be initialized the first time the function is called, and each consecutive call will return the same thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
class a {
static int b;

}

void main(void) {
a::b = 5;
}

Try that.

Share this post


Link to post
Share on other sites
The reason for not wanting a cpp file is that I want to use this class often in many of my MSVC projects. If I make a .cpp file, I have to include the .cpp file in every MSVC project I make.

I'd much rather not have to include the .cpp file every time, if I need the class, I can just "#include" it, if I decide not to use it, I can just remove the #include.

I really like the class method idea that returns a reference to the static variable.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this