Public Group

#### Archived

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

# Problem with static member variables.

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

## Recommended Posts

I'll keep it short. I am using static variables in a class, and when I try to assign the value of a paramter to a function to a static variable, I get this error:
error LNK2001: unresolved external symbol "private: static struct HINSTANCE__
* CWndProcInterface::m_hInstance" (? m_hInstance@CWndProcInterface@@0PAUHINSTANCE__@@A)

I get one of those for each of the static variables. I did a search, but there really wasn't an answer why. What does this error mean? That is all one line of course... if I didn't break it, it made the page really wide. [edited by - PlayGGY on November 17, 2003 1:22:56 AM] [edited by - PlayGGY on November 17, 2003 1:23:30 AM] [edited by - PlayGGY on November 17, 2003 1:24:07 AM] [edited by - PlayGGY on November 17, 2003 1:24:44 AM] [edited by - PlayGGY on November 17, 2003 1:24:59 AM]

##### Share on other sites
You need to declare the static variables. I usually do so at the top of my .cpp file (don''t declare in your .h file). Do something like:

int MyClass::m_myStaticVariable = 0;

##### Share on other sites
Oh...

I understand. Thanks a ton!

EDIT: Oh, wait... it says "redefinition" when I do that... maybe I should just not use statics. Its so hard though because WndProc has to be static if you are making a Windows class!

[edited by - PlayGGY on November 17, 2003 1:50:55 AM]

##### Share on other sites
quote:
Original post by OldGuy
You need to declare the static variables.
Just picking. In his case he need to define if.
quote:
Original post by PlayGGY
Oh, wait... it says "redefinition" when I do that...
That is because you''ve managed to define it in multiple compilation units. It''s hard for me to say how, but make sure you only have the definition in the header file, and the declaration in one (and ONLY one) source file.

If you need more help, please post the layout of your project: which files, and which files includes which files.

##### Share on other sites
Yes, good point, CWizard, my wording was incorrect. You declare it in the class definition, with the static keyword:

class MyClass {
static int m_myStaticVariable;
}

Usually in the .h file. Then, you define the variable once, usually in the .cpp file:

int MyClass::m_myStaticVariable;

Redefinition may be happening if you have the above definition in your .h file. If so, move it to the .cpp file where the rest of the class is defined.

EDIT: Must be too late, my brain is fried. Of course, redefinition is when you declare it one way and then define it another. So, if you added the 'static' to the above definition in the .cpp file, then it would complain of redefinition. Take the 'static' part out (which I see you already did). Sorry for the error. I'll sleep now...

[edited by - OldGuy on November 17, 2003 2:42:35 AM]

##### Share on other sites
I put it at the top of WinMain, so it couldn''t have been executed twice. Plus, if I don''t have that, and #include my header with the class definitions twice, I get no error, because I use #ifndef. As I said, I think I will just not have the functionality I was planning. I will just put WndProc straight in the Windows class, instead of in its own in the Windows class and trying to do complicated stuff with it.

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5
Rutin
12

• 12
• 9
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632112
• Total Posts
3004177

×