Jump to content
  • Advertisement

Archived

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

Kyo

Accessing a static object's function within the class's function gives error

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

I have a class, CSound, within another class CBomb. A CSound class is associated with a wav file and since every CBombs will use the same explosion.wav I need to make CSound static. It''s something like this: class CBomb { public: ... private: static CSound explosion; }; CBomb::aFunction() { explosion.playSound(); } this gives me: unresolved external symbol "private: static class CSound CBomb::sndExplosion" It won''t let me call playSound from a function of CBomb. Any idea why? And is there an easy way of loading the CSound explosion within the init member function of CBomb but making sure it''s only initialized once (i''m trying to use a static bool flag for that at the moment) thanks.

Share this post


Link to post
Share on other sites
Advertisement
You need this in the cpp file:

CSound CBomb::sndExplosion; //or some constructor arguments?

Otherwise the static member is only declared, not defined

Share this post


Link to post
Share on other sites
Thanks it works now, is it a good programming habit though? Is there another standard way of doing this?

Share this post


Link to post
Share on other sites
If you are talking about declaring the static class variable, that''s the only way. If you''re talking about static variables in general, I don''t think you''re doing it the wrong way at all. Static class variables are used when you want all instances of a class to share the same variable (literally) while maintaining class access rights. That looks exactly like what you''re doing.

Share this post


Link to post
Share on other sites

  • 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!