468,101 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

forward declaration?

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
3 20086
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
"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
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.

Similar topics

6 posts views Thread by Markus Dehmann | last post: by
2 posts views Thread by Plok Plokowitsch | last post: by
1 post views Thread by HappyHippy | last post: by
8 posts views Thread by Mohammad Omer Nasir | last post: by
4 posts views Thread by Steve | last post: by
7 posts views Thread by RedLars | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.