Sign in to follow this  
steenreem

Class reference error

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this