Chat on IRC |
Static classes
Posted by masonium [send private reply] at December 29, 2001, 12:58:54 AM
I'm using MSVC++ 6.0, just in case it matters
I have a class that looks something like this: class A { public: static float GetStuff(void) { return stuff; } protected: static float stuff; }; Whenever I try to access the function via A::GetStuff(), the compiler gives me this error: error LNK2001: unresolved external symbol "protected: static float A::stuff" (?stuff@A@@1MA) Any ideas why?
Posted by CodeRed [send private reply] at December 29, 2001, 01:12:20 AM
Your declaring that as a static function, call it as GetStuff(); (you do not need the class name in front of it)
And I'm pretty sure there is no such thing as a static class
Posted by masonium [send private reply] at December 29, 2001, 01:29:30 AM
thanks, the title was supposed to be 'static functions in classes' but i forgot a couple of words
Posted by Psion [send private reply] at December 29, 2001, 10:13:43 AM
I'll be surprised if CodeRed's advice helped you!
You really do need to call it the way you were, and your class declaration looks fine. The problem is that you are apparently never defining A::GetStuff anywhere. You need to have one (and only one) C++ source file contain the definition of this function.
Posted by CodeRed [send private reply] at December 29, 2001, 12:21:43 PM
Oh.. right you do need to call it with the class name, just not an object name. Thats what 3 weeks away from school will do to you.
Posted by CViper [send private reply] at December 30, 2001, 06:03:27 PM
i think you need to "initialize" stuff... (not really initialize, since you dont have to pass it anything) just type
float A::stuff; right after the class declaration... you could also set it to something: float A::stuff = 5.0f; or call the constructor of a static class... if stuff would be a class float A::stuff( <constructor params here> ); Good new year BTW :)
Posted by Psion [send private reply] at December 30, 2001, 06:17:57 PM
Oops. Misread the error as saying the function wasn't found instead of the variable. Thanks ;-)
Posted by unknown_lamer [send private reply] at January 02, 2002, 08:12:31 PM
the variable shouldn't work either. You must initialize it exactly once outside of the class. best place to do this is right after the declaration in the header. e.g. A::stuff = 5.0;
Posted by Psion [send private reply] at January 02, 2002, 08:37:44 PM
... he already said that
Register as a new user | |||||||||||