# Basic class pointer question

## Recommended Posts

troutofdoom    122
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 on other sites
ToohrVyk    1595
Sanity check: your class definition does end with a semicolon ( class {}; ) ?

##### Share on other sites
troutofdoom    122
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 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 on other sites
Oluseyi    2103
Quote:
 Original post by troutofdoom// in global.h#include "cSprite.h"extern cSprite *sprite;...// in sprite.hclass 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.hclass Sprite;extern Sprite * sprite;// global.cppSprite * 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 PosterHow 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 on other sites
troutofdoom    122
Thank you both for the help. I should have realized that the compiler needed to know it was a class.