Sign in to follow this  

Basic class pointer question

This topic is 4135 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

Hi, sorry.. I know questions that people should know come up all the time here and you grow tired of them, but I just don't know what I'm doing wrong. I'm making a project and I want to be able to call a classes' function/public variables from outside the main loop and it's own scope. For example have an enemy damage a player without using globals. It seems to me that a pointer declared in global.h/cpp would be the best way but I cannot get it to work.. as an example:
// in global.h
#include "cSprite.h"
extern cSprite *sprite;

// in global.cpp
cSprite *sprite = NULL;

// in main
sprite->animate();

// in sprite.h
class cSprite
{..}
And I get an error in global.h at extern cSprite *sprite; "error: expected initializer before '*' token" I know I'm just missing something stupid, but I've googled, looked at others' source code, etc. and can't find an answer. Thank you..

Share this post


Link to post
Share on other sites
Yep, I forgot to include that in my post.

The code works fine if I take out pointers and just declare an instance inside main. But after writing code to handle damage events with a global mediator, I realized that this nonsense will quickly get out of hand and I need to organize.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How does [extern class cSprite *sprite;] work? I too had this problem once but i forgot if this is how i fixed it.

Share this post


Link to post
Share on other sites
Quote:
Original post by troutofdoom
// in global.h
#include "cSprite.h"
extern cSprite *sprite;

...

// in sprite.h
class cSprite
{..}

I'm assuming that's a typo and the two underlined filenames actually match? In any case, global.h doesn't need to include Sprite.h (please stop prefixing your class names with 'c'; it's actually a relic of very old IDEs and there's no longer any reason to do it). Simply use a forward declaration:
// global.h
class Sprite;
extern Sprite * sprite;

// global.cpp
Sprite * sprite = NULL;

This minimizes dependencies, and doesn't force every file that includes global.h to be invalidated (and thus recompiled) whenever sprite.h changes.

The error message indicates that the compiler doesn't find the type declaration for cSprite in global.h, and thus thinks it's a variable identifier and expects an initializer or end of line. Of course, even if it were a variable identifier, it'd still barf because then it would complain about a missing type.

Quote:
Original post by Anonymous Poster
How does [extern class cSprite *sprite;] work? I too had this problem once but i forgot if this is how i fixed it.

extern tells the compiler that this object is defined externally - in another file - so just consider this a declaration. It is the only way to declare variables without simultaneously defining them.

Share this post


Link to post
Share on other sites

This topic is 4135 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.

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