forward reference to a class...

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

    forward reference to a class...

    Hi,

    I'm not able to make a forward reference to a class, so there's something I
    don't know... Can you help me?

    Here's the example...

    /*************** *************** *************** ***************
    Programma: File: Funzione: Descrizione: Autore:
    Ambiente: Dev-C++ 4.9.8.1, Athlon/750 128mb RAM, Windows 98
    Note: Revisioni: 16/09/03 11.05
    *************** *************** *************** ***************/
    #include <iostream>

    class B;

    class A {
    public:
    B first;
    };

    class B {
    public:
    A second;
    };

    int main() {
    system("PAUSE") ;
    return 0;
    }


    --------------------------------------------------------

    Thanks in advance,
    Checco.


  • Jacek Dziedzic

    #2
    Re: forward reference to a class...

    Might try storing pointers to classes as members instead.

    HTH,
    - J.


    Comment

    • Nils Petter Vaskinn

      #3
      Re: forward reference to a class...

      On Tue, 16 Sep 2003 09:08:34 +0000, Francesco wrote:
      [color=blue]
      > Hi,
      >
      > I'm not able to make a forward reference to a class, so there's something I
      > don't know... Can you help me?
      >
      > class B;
      >
      > class A {
      > public:
      > B first;
      > };
      >
      > class B {
      > public:
      > A second;
      > };[/color]


      A contains a B which contains an A which contains a B which contains an A
      which ......

      See where we're going?

      Your problem isn't that you can't tell the compiler what to do, it's that
      you are asking it to do something impossible. You probably want first and
      second to be pointers, but without telling us what A and/or B are for we
      can't know.

      What are A and B supposed to be/do ?

      --
      NPV
      "Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

      Comment

      • Rob Williscroft

        #4
        Re: forward reference to a class...

        Francesco wrote in news:oprvk4zu0y rrafxh@news.tin .it:
        [color=blue]
        > Hi,
        >
        > I'm not able to make a forward reference to a class, so there's
        > something I don't know... Can you help me?[/color]

        I've no doubt there is a faq entry for this. Oh here it is:

        38.12

        To avoid the wrap: http://tinyurl.com/niru

        [color=blue]
        >
        > Here's the example...
        >
        > /*************** *************** *************** ***************
        > Programma: File: Funzione: Descrizione: Autore:
        >
        > Ambiente: Dev-C++ 4.9.8.1, Athlon/750 128mb RAM, Windows 98
        > Note: Revisioni: 16/09/03 11.05
        > *************** *************** *************** ***************/
        > #include <iostream>
        >
        > class B;
        >
        > class A {
        > public:
        > B first;
        > };[/color]

        What's the size of A ?
        [color=blue]
        >
        > class B {
        > public:
        > A second;
        > };
        >[/color]

        What's the size of B ?
        [color=blue]
        > int main() {
        > system("PAUSE") ;
        > return 0;
        > }
        >[/color]

        Both A and B above if the above code was legal would have infanite
        size ( A includes B wich includes A ... ).

        Which is probably why the language requires a complete type,
        one which has a body ( the bit in {}; for a class ) if you want
        to have a member of that type. References, pointers and static
        members are OK though as the defenition (particulary sizeof info')
        isn't required till later.

        HTH

        Rob.
        --

        Comment

        • Francesco

          #5
          Re: forward reference to a class...

          On Tue, 16 Sep 2003 10:38:42 GMT, Nils Petter Vaskinn
          <no@spam.for.me .invalid> wrote:

          [color=blue]
          >
          > Your problem isn't that you can't tell the compiler what to do, it's that
          > you are asking it to do something impossible. You probably want first and
          > second to be pointers, but without telling us what A and/or B are for we
          > can't know.
          >
          > What are A and B supposed to be/do ?
          >[/color]

          I'm learning C++ and this was only an exercice to take confidence with
          class. But I've understood my mistake and effetively pointers are the right
          way to make a mutual association between two object.

          Thanks,
          Checco.

          Comment

          • Francesco

            #6
            Re: forward reference to a class...

            Thank to all,
            I've understood my mistake.

            Thanks,
            Checco.

            Comment

            Working...