By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

forward declaration?

P: n/a
Hello
with the original code below I get the error:
"forward declaration of `struct CPlayer'"

class CPlayer; // what does this do? Instantiate the class CPlayer?
// what's a forward declaration?

class CEnemy : public CEntity
{
public:
CPlayer *player;
}

I tried it like this which brought the error:
"error: syntax error before `*' token"

class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}

THANKS for you help and regards.
Michael
Sep 27 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael Sgier wrote:
Hello
with the original code below I get the error:
"forward declaration of `struct CPlayer'"

class CPlayer; // what does this do? Instantiate the class CPlayer?
No. It tells the compiler that CPlayer is a class. This is necessary if you
have classes that refer to each other, or you can do it in some
circumstances simply to avoid #including the header that defines the class,
which can speed up compilation.
// what's a forward declaration?
The declaration for CPlayer above is a forward declaration, i.e., you've
specified it as a class ahead of the full class definition, which will be
somewhere else.

class CEnemy : public CEntity
{
public:
CPlayer *player;
}
Missing semi-colon.

There must be other code that you compiled but did not post. Where is the
definition of CEntity? Also, to get an error for 'struct CPlayer' suggests
that CPlayer is really a struct, not a class. I'm guessing that the source
file you compiled #includes the full definition of CPlayer, which is a
struct, but the forward declaration says it's a class, hence the error
message.

You can use a forward declaration if you only refer to pointers or
references to CPlayer (which is the case here), but you need the entire
definition of CPlayer in other cases, such as defining a member object of
type CPlayer in CEnemy (as opposed to a pointer or reference), or accessing
any member of CPlayer, etc.

I tried it like this which brought the error:
"error: syntax error before `*' token"
Yes, because the compiler does not know what CPlayer is. You need a forward
declaration of CPlayer, the full definition of CPlayer, and you need the
full definitionof CEntity.
class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}


Another missing semi-colon.

DW
Sep 27 '05 #2

P: n/a
"David White" <no@email.provided> wrote in message
news:vJ******************@nasal.pacific.net.au...
Yes, because the compiler does not know what CPlayer is. You need a forward declaration of CPlayer,
Correction: Insert the word "or" here.
the full definition of CPlayer, and you need the
full definitionof CEntity.


DW

Sep 27 '05 #3

P: n/a
The following article demonstrates the use of forward declarations to
simplify header file management.

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Modeling Tool

Sep 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.