Jump to content
  • Advertisement
Sign in to follow this  
Timberl

linking problem with static variables (g++)

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

Hi boys & girls, maybe someone can see what I'm doing wrong here because I can't figure it out The error messages are: LOCALS~1/Temp/ccHASGA8.o(.text+0x81):foo.cc: undefined reference to `Foo::setInstance(Foo*)' LOCALS~1/Temp/ccHASGA8.o(.text+0xa9):foo.cc: undefined reference to `Foo::setInstance(Foo*)' which I'm pretty sure is a linking error... but why am I causing a linking error? any help much appreciated this is my source: =======================Foo.h #include<stdio.h> #include<iostream> //-----------------[ Foo.h ]----------------- class Foo{ private: static Foo* instance; public: virtual void printOut(); static Foo* getInstance(); protected: void static setInstance( Foo* f ); }; //-----------------[ SuperFoo.h ]----------------- class SuperFoo : public Foo{ public: virtual void printOut(); SuperFoo(); }; =======================Foo.cc #include "foo.h" //----------------[ Foo.cc ]---------- Foo* Foo::instance = NULL; void Foo::printOut(){ std::cout<< "this is Foo"; } Foo* Foo::getInstance(){ if( instance == NULL ){ setInstance( new Foo() ); } return instance; } //------------------[ SuperFoo.cc ]------------- SuperFoo::SuperFoo(){ setInstance( this ); } void SuperFoo::printOut(){ std::cout << "This is Super-Foo!"; } //-----------------[ Main.cc ]----------------- int main( char** args, int n){ Foo::getInstance()->printOut(); Foo* f = new SuperFoo(); // the instance pointer in Foo should have been replaced with the new SuperFoo object pointer Foo::getInstance()->printOut(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Nope, the error just speaks for itself. You don't have setInstance implemented.

Also, I haven't used private variables in my entire life(just didn't need them), so here's my question:
What would compiler say for SuperFoo calling setInstance, when setInstance is using private variable of Foo? Wouldn't it scream that setInstance should be private as well(and next to it - getInstance)?
/def

Share this post


Link to post
Share on other sites
compiler wouldn't care. setInstance is protected so super-classes (e.g. superFoo) can access it.

private's are useful to force people to use the accessor methods you provide, which is alot safer and maintainable than exposing a field to be man-handled directly by extending classes. I personally never have a class-field as anything other than private unless I'm doing something hacky that really needs speed. just an OO attitude really.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!