Class reference error

Started by
7 comments, last by Beskrajnost 15 years, 9 months ago
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?
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.
BTW, you are currently using private inheritance, which is probably not what you want. To use public inheritance, write:

struct Hero: public BattleObject { //...
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.
Quote:Original post by rip-off
In addition to default public member access, structs default to public inheritance.


Learn something new every day. [smile]
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.
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.
The member access syntax (via the dot operator) is actually quite legal for static members, if strange.
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]

This topic is closed to new replies.

Advertisement