• Advertisement
Sign in to follow this  

Class reference error

This topic is 3517 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 this class: struct Hero: BattleObject { static float hull, fuel; static float energy, shield, lastTorpedoFireTime, lastPhaserFireTime; }; And I get this error: C:\Users\Remy\Documents\CPP Enterprise game2\main.cpp|576|undefined reference to `Hero::lastPhaserFireTime'| at this line " hero.lastPhaserFireTime=((float)(time(NULL)))/1000; " which points to a global variable: " Hero hero; " How come?

Share this post


Link to post
Share on other sites
Advertisement
Static member variables need to be defined outside the class. However, there's no reason for them to be static here, so just don't make them static.

Share this post


Link to post
Share on other sites
BTW, you are currently using private inheritance, which is probably not what you want. To use public inheritance, write:

struct Hero: public BattleObject { //...

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
BTW, you are currently using private inheritance, which is probably not what you want. To use public inheritance, write:

struct Hero: public BattleObject { //...


In addition to default public member access, structs default to public inheritance. But for clarity it is usually a good idea to include the public anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
In addition to default public member access, structs default to public inheritance.


Learn something new every day. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Static member variables need to be defined outside the class. However, there's no reason for them to be static here, so just don't make them static.


By way of qualification to the original poster, static member variables are essentially shared across all instances of the class -- they exist once and only once. That means (in this case) no matter how many Hero objects you create, they will all share a single hull and energy value (for example). This is not likely to be what you want, especially given you example code line (and if it is, it is not a particularly good design).

So in this case, as ToohrVyk says, the solution you want is to remove the static qualifier.

Share this post


Link to post
Share on other sites
Or, if you are certainly sure that you need to qualify those aforementioned variables as static, member access operator is not the way to go. You have to use the scope resolution operator and thus the erroneous line of code becomes the following:
Hero::lastPhaserFireTime=((float)(time(NULL)))/1000;

But I'm quite sure this is not really what you want, though just squeeze it in as a side note.

Share this post


Link to post
Share on other sites
The member access syntax (via the dot operator) is actually quite legal for static members, if strange.

Share this post


Link to post
Share on other sites
Ouch my bad, thought he was trying to access via the class name, not via an instance. I should have read the post better...

[Edit]
This is why Hungarian notation is good for eh? If he said hroHero.memberName, everything would have been better. :P

That said, static members must be initialized outside the class body. Are you sure you're doing that? Double-check!

[Edited by - Beskrajnost on July 7, 2008 10:20:02 AM]

Share this post


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

  • Advertisement