Jump to content
  • Advertisement
Sign in to follow this  
PB Baller27

Declaring Private Static Int Member Variable and Usage !?!!?!

This topic is 4423 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

ok yet again I have a problem with my code. An example of my code and logic is below. MyClass.h
class MyClass
{
public:
  MyClass();
  void function1();
private:
   static int var1;
};


Class.CPP
MyClass::MyClass
   {
     var1 = 0
   }

void function1()
   {
      var1 = 15;
   }



with an error of //--------------------------------------------------------------\ - Linking... - GameMap.obj : error LNK2019: unresolved external symbol - "private: static int GameMap::mi_currentMap" (?mi_currentMap@GameMap@@0HA) referenced in function "public: int __thiscall GameMap::getMap(void)" (?getMap@GameMap@@QAEHXZ) \\--------------------------------------------------------------// Basically It just doesnt like the private static variable. So how do I make it like it? Lastly, I would just like to say thank you to everyone here. The people here in this community have really helped me out (Alot). This site is great and I just wanted to say thanks.

Share this post


Link to post
Share on other sites
Advertisement
The linker error indicates that the variable has been declared (thus silencing the compiler) but not defined (thus causing the linker to barf).

Add int MyClass::var1; to your CPP file to actually create the variable.

Share this post


Link to post
Share on other sites
It WORKED!! :) Dont you just hate the little things that screw everything up. Thanks again. But curious why do you have to go int MyClass::var1; instead of static int MyClass::var1;? THANKS

Share this post


Link to post
Share on other sites
Quote:
Original post by PB Baller27
It WORKED!! :) Dont you just hate the little things that screw everything up. Thanks again. But curious why do you have to go int MyClass::var1; instead of static int MyClass::var1;? THANKS


At file scope, the keyword static means that the symbol has internal linkage and is thus invisible from other translation units. This meaning is inherited from C.

Share this post


Link to post
Share on other sites
Quote:
Original post by PB Baller27
It WORKED!! :) Dont you just hate the little things that screw everything up.

No. I usually do RTFM :)

Quote:
But curious why do you have to go int MyClass::var1; instead of static int MyClass::var1;? THANKS

Because symbol MyClass::var1 is already defined (with static keyword) in .h file, so you need only to declare it (where to reserve memory) and compiler will know he need to declare it (static/inline/..).

Share this post


Link to post
Share on other sites
Quote:
Original post by bubu LV
Because symbol MyClass::var1 is already defined (with static keyword) in .h file, so you need only to declare it (where to reserve memory) and compiler will know he need to declare it (static/inline/..).


It's the other way around.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!