Forward References

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

    Forward References

    I'm having a problem with forward references.

    For example,

    class DATE;
    class MYCLASS;

    class MYCLASS
    {
    public:
    int n;
    DATE date;
    };

    class DATE
    {
    public:
    int day;
    int month;
    int year;
    };

    int main(int argc, char* argv)
    {
    return 0;
    }


    results in


    g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
    impmain.cpp:8: field `date' has incomplete type
    make: *** [pc/impmain.o] Error 1

    Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27


    Why? I've declared the forward reference at the beginning of the file. Is
    this just a stupidity of g++??
    --
    % Randy Yates % "Watching all the days go by...
    %% Fuquay-Varina, NC % Who are you and who am I?"
    %%% 919-577-9882 % 'Mission (A World Record)',
    %%%% <yates@ieee.org > % *A New World Record*, ELO

  • Randy Yates

    #2
    Re: Forward References

    Randy Yates <yates@ieee.org > writes:
    [color=blue]
    > I'm having a problem with forward references.
    >
    > For example,
    >
    > class DATE;
    > class MYCLASS;
    >
    > class MYCLASS
    > {
    > public:
    > int n;
    > DATE date;
    > };
    >
    > class DATE
    > {
    > public:
    > int day;
    > int month;
    > int year;
    > };
    >
    > int main(int argc, char* argv)
    > {
    > return 0;
    > }
    >
    >
    > results in
    >
    >
    > g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
    > impmain.cpp:8: field `date' has incomplete type
    > make: *** [pc/impmain.o] Error 1
    >
    > Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
    >
    >
    > Why? I've declared the forward reference at the beginning of the file. Is
    > this just a stupidity of g++??[/color]

    I forgot to add that if I switch the order of the class declarations
    the error goes away. I shouldn't have to order class declarations
    which have forward references in my files, just as I don't have to
    order subroutines if they have prototype definitions.
    --
    % Randy Yates % "The dreamer, the unwoken fool -
    %% Fuquay-Varina, NC % in dreams, no pain will kiss the brow..."
    %%% 919-577-9882 %
    %%%% <yates@ieee.org > % 'Eldorado Overture', *Eldorado*, ELO

    Comment

    • JKops

      #3
      Re: Forward References

      Randy Yates posted:
      [color=blue]
      > I'm having a problem with forward references.
      >
      > For example,
      >
      > class DATE;
      > class MYCLASS;
      >
      > class MYCLASS
      > {
      > public:
      > int n;
      > DATE date;
      > };
      >
      > class DATE
      > {
      > public:
      > int day;
      > int month;
      > int year;
      > };
      >
      > int main(int argc, char* argv)
      > {
      > return 0;
      > }[/color]


      Rearrange it:

      (Plus I corrected a typo, it should've been "argv[]")


      class DATE
      {
      public:
      int day;
      int month;
      int year;
      };

      class MYCLASS
      {
      public:
      int n;
      DATE date;
      };


      int main(int argc, char* argv[])
      {
      return 0;
      }


      -JKop

      Comment

      • Julián Albo

        #4
        Re: Forward References

        Randy Yates wrote:
        [color=blue]
        > Why? I've declared the forward reference at the beginning of the file. Is
        > this just a stupidity of g++??[/color]

        Forward declaration allow only to declare pointer or references, to declare
        an object of the class the complete declaration is required.

        --
        Salu2

        Comment

        • John Harrison

          #5
          Re: Forward References


          "Randy Yates" <yates@ieee.org > wrote in message
          news:u0sud2h6.f sf@ieee.org...[color=blue]
          > I'm having a problem with forward references.
          >
          > For example,
          >
          > class DATE;
          > class MYCLASS;
          >
          > class MYCLASS
          > {
          > public:
          > int n;
          > DATE date;
          > };
          >
          > class DATE
          > {
          > public:
          > int day;
          > int month;
          > int year;
          > };
          >
          > int main(int argc, char* argv)
          > {
          > return 0;
          > }
          >
          >
          > results in
          >
          >
          > g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
          > /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
          > impmain.cpp:8: field `date' has incomplete type
          > make: *** [pc/impmain.o] Error 1
          >
          > Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
          >
          >
          > Why? I've declared the forward reference at the beginning of the f ile.Is
          > this just a stupidity of g++??[/color]

          C++ is no different from C in this regard. Replace class with struct, remove
          'public:', and you'd have a C program and exactly the same error. You are a
          C programmer, no?

          In order to compile MYCLASS the compiler needs some basic information, such
          as the size of each data member. It cannot get that from a forward
          declaration.

          class DATE;

          class MYCLASS
          {
          public:
          int n;
          DATE* date;
          };

          This is OK because DATE* has a known size; but what you wrote is not because
          the compiler does not know the size of DATE.

          Suggest you simply reorder the classes.

          john


          Comment

          • David Lindauer

            #6
            Re: Forward References



            Randy Yates wrote:
            [color=blue]
            > I'm having a problem with forward references.
            >
            > For example,
            >
            > class DATE;
            > class MYCLASS;
            >
            > class MYCLASS
            > {
            > public:
            > int n;
            > DATE date;
            > };
            >
            > class DATE
            > {
            > public:
            > int day;
            > int month;
            > int year;
            > };
            >
            > int main(int argc, char* argv)
            > {
            > return 0;
            > }
            >
            > results in
            >
            > g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
            > impmain.cpp:8: field `date' has incomplete type
            > make: *** [pc/impmain.o] Error 1
            >
            > Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
            >
            > Why? I've declared the forward reference at the beginning of the file. Is
            > this just a stupidity of g++??
            > --
            > % Randy Yates % "Watching all the days go by...
            > %% Fuquay-Varina, NC % Who are you and who am I?"
            > %%% 919-577-9882 % 'Mission (A World Record)',
            > %%%% <yates@ieee.org > % *A New World Record*, ELO
            > http://home.earthlink.net/~yatescr[/color]

            when the compiler is compiling 'myclass' it runs into a forward declared reference to 'date'. However, while date is known to
            exist, the compiler does not know anything about it, such as size or layout. But at this phase of the compilation process, the
            compiler needs to know at *least* the size, and possibly more so that it can complete the declaration of myclass. That means that
            to use date here, you had to have previously done more than forward declaring it.

            The forward declarations are primarily used to declare types that are going to be used as pointer references
            in subsequent classes, this way you can have classes that point to each other in a circular fashion.

            David

            Comment

            • Randy Yates

              #7
              Re: Forward References

              "John Harrison" <john_andronicu s@hotmail.com> writes:
              [color=blue]
              > "Randy Yates" <yates@ieee.org > wrote in message
              > news:u0sud2h6.f sf@ieee.org...[color=green]
              >> I'm having a problem with forward references.
              >>
              >> For example,
              >>
              >> class DATE;
              >> class MYCLASS;
              >>
              >> class MYCLASS
              >> {
              >> public:
              >> int n;
              >> DATE date;
              >> };
              >>
              >> class DATE
              >> {
              >> public:
              >> int day;
              >> int month;
              >> int year;
              >> };
              >>
              >> int main(int argc, char* argv)
              >> {
              >> return 0;
              >> }
              >>
              >>
              >> results in
              >>
              >>
              >> g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
              >> /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
              >> impmain.cpp:8: field `date' has incomplete type
              >> make: *** [pc/impmain.o] Error 1
              >>
              >> Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
              >>
              >>
              >> Why? I've declared the forward reference at the beginning of the f ile.Is
              >> this just a stupidity of g++??[/color]
              >
              > C++ is no different from C in this regard. Replace class with struct, remove
              > 'public:', and you'd have a C program and exactly the same error. You are a
              > C programmer, no?[/color]

              Yes, and structure definitions have no such thing as a "forward reference," and
              if they did I'd be asking the same question.

              If a forward reference isn't good for this, then what is it good for?
              --
              % Randy Yates % "...the answer lies within your soul
              %% Fuquay-Varina, NC % 'cause no one knows which side
              %%% 919-577-9882 % the coin will fall."
              %%%% <yates@ieee.org > % 'Big Wheels', *Out of the Blue*, ELO

              Comment

              • Mike Wahler

                #8
                Re: Forward References


                "Randy Yates" <yates@ieee.org > wrote in message
                news:3c0edjzg.f sf@ieee.org...[color=blue]
                > "John Harrison" <john_andronicu s@hotmail.com> writes:
                >[color=green]
                > > "Randy Yates" <yates@ieee.org > wrote in message
                > > news:u0sud2h6.f sf@ieee.org...[color=darkred]
                > >> I'm having a problem with forward references.
                > >>
                > >> For example,
                > >>
                > >> class DATE;
                > >> class MYCLASS;
                > >>
                > >> class MYCLASS
                > >> {
                > >> public:
                > >> int n;
                > >> DATE date;
                > >> };
                > >>
                > >> class DATE
                > >> {
                > >> public:
                > >> int day;
                > >> int month;
                > >> int year;
                > >> };
                > >>
                > >> int main(int argc, char* argv)
                > >> {
                > >> return 0;
                > >> }
                > >>
                > >>
                > >> results in
                > >>
                > >>
                > >> g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I
                > >> /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
                > >> impmain.cpp:8: field `date' has incomplete type
                > >> make: *** [pc/impmain.o] Error 1
                > >>
                > >> Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
                > >>
                > >>
                > >> Why? I've declared the forward reference at the beginning of the f[/color][/color][/color]
                ile.Is[color=blue][color=green][color=darkred]
                > >> this just a stupidity of g++??[/color]
                > >
                > > C++ is no different from C in this regard. Replace class with struct,[/color][/color]
                remove[color=blue][color=green]
                > > 'public:', and you'd have a C program and exactly the same error. You[/color][/color]
                are a[color=blue][color=green]
                > > C programmer, no?[/color]
                >
                > Yes, and structure definitions have no such thing as a "forward[/color]
                reference,"

                Nor do class definitions. But in C, yes a forward reference to
                a struct can indeed be declared.

                The following is valid C (as well as C++):

                struct x; /* forward declare type 'struct x' */
                struct x *p; /* define a pointer to type 'struct x' */

                /* (but an actual object of type 'struct x' cannot be
                created until after type 'struct x' is completely defined) */

                struct x s; /* invalid */

                /* fully define type 'struct x' */
                struct x
                {
                int a;
                };

                struct x s2; /* valid */
                [color=blue]
                > and
                > if they did I'd be asking the same question.[/color]

                [color=blue]
                >
                > If a forward reference isn't good for this, then what is it good for?[/color]

                For defining a pointer (or reference in C++) to a struct or class which
                has yet to be defined.

                This issue works exactly the same way in C as in C++

                -Mike



                Comment

                • John Harrison

                  #9
                  Re: Forward References

                  >>[color=blue][color=green]
                  >> C++ is no different from C in this regard. Replace class with struct,
                  >> remove
                  >> 'public:', and you'd have a C program and exactly the same error. You are
                  >> a
                  >> C programmer, no?[/color]
                  >
                  > Yes, and structure definitions have no such thing as a "forward
                  > reference," and
                  > if they did I'd be asking the same question.
                  >[/color]

                  Not true.

                  struct Node;

                  That is perfectly good C and C++.
                  [color=blue]
                  > If a forward reference isn't good for this, then what is it good for?[/color]

                  It tells the compiler that a name is the name of a class (or struct). You
                  can do a few things with just a name, for instance you can declare a pointer
                  or a reference. For instance

                  struct Node;

                  class List
                  {
                  public:
                  void add_node(Node*) ;
                  void remove_node(Nod e*);
                  };

                  John


                  Comment

                  • Randy Yates

                    #10
                    Re: Forward References

                    Thanks David, John, and Mike. I guess I see the point now, but
                    I don't see why the compiler couldn't have been designed to
                    forward scan the file and pick up that information without
                    the user having to go through these hoops - something like
                    a "two-pass" compiler design.

                    --RY


                    David Lindauer <camille@bluegr ass.net> writes:
                    [color=blue]
                    > Randy Yates wrote:
                    >[color=green]
                    >> I'm having a problem with forward references.
                    >>
                    >> For example,
                    >>
                    >> class DATE;
                    >> class MYCLASS;
                    >>
                    >> class MYCLASS
                    >> {
                    >> public:
                    >> int n;
                    >> DATE date;
                    >> };
                    >>
                    >> class DATE
                    >> {
                    >> public:
                    >> int day;
                    >> int month;
                    >> int year;
                    >> };
                    >>
                    >> int main(int argc, char* argv)
                    >> {
                    >> return 0;
                    >> }
                    >>
                    >> results in
                    >>
                    >> g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/impmain.o -Wno-deprecated impmain.cpp
                    >> impmain.cpp:8: field `date' has incomplete type
                    >> make: *** [pc/impmain.o] Error 1
                    >>
                    >> Compilation exited abnormally with code 2 at Sat Oct 16 12:54:27
                    >>
                    >> Why? I've declared the forward reference at the beginning of the file. Is
                    >> this just a stupidity of g++??
                    >> --
                    >> % Randy Yates % "Watching all the days go by...
                    >> %% Fuquay-Varina, NC % Who are you and who am I?"
                    >> %%% 919-577-9882 % 'Mission (A World Record)',
                    >> %%%% <yates@ieee.org > % *A New World Record*, ELO
                    >> http://home.earthlink.net/~yatescr[/color]
                    >
                    > when the compiler is compiling 'myclass' it runs into a forward declared reference to 'date'. However, while date is known to
                    > exist, the compiler does not know anything about it, such as size or layout. But at this phase of the compilation process, the
                    > compiler needs to know at *least* the size, and possibly more so that it can complete the declaration of myclass. That means that
                    > to use date here, you had to have previously done more than forward declaring it.
                    >
                    > The forward declarations are primarily used to declare types that are going to be used as pointer references
                    > in subsequent classes, this way you can have classes that point to each other in a circular fashion.
                    >
                    > David[/color]

                    --
                    % Randy Yates % "My Shangri-la has gone away, fading like
                    %% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
                    %%% 919-577-9882 %
                    %%%% <yates@ieee.org > % 'Shangri-La', *A New World Record*, ELO

                    Comment

                    • John Harrison

                      #11
                      Re: Forward References


                      "Randy Yates" <yates@ieee.org > wrote in message
                      news:k6tpcjjx.f sf@ieee.org...[color=blue]
                      > Thanks David, John, and Mike. I guess I see the point now, but
                      > I don't see why the compiler couldn't have been designed to
                      > forward scan the file and pick up that information without
                      > the user having to go through these hoops - something like
                      > a "two-pass" compiler design.
                      >[/color]

                      One of the points of forward references is when the real definition is in a
                      different file and you don't want the compiler to have to compile the real
                      definition. This is useful in header files to cut down on dependencies
                      between header files.

                      john


                      Comment

                      • Mike Wahler

                        #12
                        Re: Forward References


                        "Randy Yates" <yates@ieee.org > wrote in message
                        news:k6tpcjjx.f sf@ieee.org...[color=blue]
                        > Thanks David, John, and Mike. I guess I see the point now, but
                        > I don't see why the compiler couldn't have been designed to
                        > forward scan the file and pick up that information without
                        > the user having to go through these hoops - something like
                        > a "two-pass" compiler design.[/color]

                        This isn't an issue of compiler design, but language design.

                        Consider that C++ (like C) is designed to allow separate
                        compilation of modules of the same ultimate application.
                        Forward scan *which* file? (the needed one might not, and
                        often does not, yet exist).

                        -Mike


                        Comment

                        Working...