forward declaration?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Sgier

    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
  • David White

    #2
    Re: forward declaration?

    Michael Sgier wrote:[color=blue]
    > 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?[/color]

    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.
    [color=blue]
    > // what's a forward declaration?[/color]

    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.
    [color=blue]
    >
    > class CEnemy : public CEntity
    > {
    > public:
    > CPlayer *player;
    > }[/color]

    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.
    [color=blue]
    >
    > I tried it like this which brought the error:
    > "error: syntax error before `*' token"[/color]

    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.
    [color=blue]
    > class CEnemy : public CEntity
    > {
    > public:
    > // CPlayer *player;
    > CPlayer* player = new CPlayer;
    > }[/color]

    Another missing semi-colon.

    DW


    Comment

    • David White

      #3
      Re: forward declaration?

      "David White" <no@email.provi ded> wrote in message
      news:vJ5_e.1205 $96.126568@nasa l.pacific.net.a u...[color=blue]
      > Yes, because the compiler does not know what CPlayer is. You need a[/color]
      forward[color=blue]
      > declaration of CPlayer,[/color]

      Correction: Insert the word "or" here.
      [color=blue]
      > the full definition of CPlayer, and you need the
      > full definitionof CEntity.[/color]

      DW



      Comment

      • EventHelix.com

        #4
        Re: forward declaration?

        The following article demonstrates the use of forward declarations to
        simplify header file management.



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

        Comment

        Working...