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

Started by
3 comments, last by Kyo 21 years, 3 months ago
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.
Advertisement
The CBomb::sndExplosion() function is missing from the CBomb class. (cpp file)
You need this in the cpp file:

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

Otherwise the static member is only declared, not defined
Thanks it works now, is it a good programming habit though? Is there another standard way of doing this?
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.

This topic is closed to new replies.

Advertisement