Class forward declaration

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

    Class forward declaration

    Hello,

    I have the definitions of classes A and B in a header file. Class A has a
    private member of type B. Class B is defined after class A in the header
    file. I place a forward declaration of class B above class A but VS.NET
    still complains with:

    error C2079: 'TextDB::TextDB ::database_vers ion' uses undefined class
    'TextDB::TextDB _Version'

    namespace TextDB
    {
    class TextDB_Version;

    class TextDB
    {
    private:
    TextDB_Version database_versio n;
    };

    class TextDB_Version
    {
    [...]
    };

    } // namespace TextDB

    Is this because this is in a header file? I'm confused.

    Thanks a lot,
    Kevin Grigorenko


  • Cy Edmunds

    #2
    Re: Class forward declaration

    "Kevin Grigorenko" <kzg110@psu.edu > wrote in message
    news:bk3dik$t1m $1@f04n12.cac.p su.edu...[color=blue]
    > Hello,
    >
    > I have the definitions of classes A and B in a header file. Class A has a
    > private member of type B. Class B is defined after class A in the header
    > file. I place a forward declaration of class B above class A but VS.NET
    > still complains with:
    >
    > error C2079: 'TextDB::TextDB ::database_vers ion' uses undefined class
    > 'TextDB::TextDB _Version'
    >
    > namespace TextDB
    > {
    > class TextDB_Version;
    >
    > class TextDB
    > {
    > private:
    > TextDB_Version database_versio n;[/color]

    How much space should the compiler allocate for this class? It can't know at
    this point. It would work to say

    TextDB_Version *pdatabase_vers ion;

    because all pointers have the same size.
    [color=blue]
    > };
    >
    > class TextDB_Version
    > {
    > [...]
    > };
    >
    > } // namespace TextDB
    >
    > Is this because this is in a header file? I'm confused.
    >
    > Thanks a lot,
    > Kevin Grigorenko
    >[/color]

    --
    Cy



    Comment

    • Phlip

      #3
      Re: Class forward declaration

      > I have the definitions of classes A and B in a header file. Class A has a[color=blue]
      > private member of type B. Class B is defined after class A in the header
      > file. I place a forward declaration of class B above class A but VS.NET
      > still complains with:
      >
      > error C2079: 'TextDB::TextDB ::database_vers ion' uses undefined class
      > 'TextDB::TextDB _Version'
      >
      > namespace TextDB
      > {
      > class TextDB_Version;
      >
      > class TextDB
      > {
      > private:
      > TextDB_Version database_versio n;
      > };
      >
      > class TextDB_Version
      > {
      > [...]
      > };
      >
      > } // namespace TextDB
      >
      > Is this because this is in a header file? I'm confused.[/color]

      C++ obeys parsing rules that behave as-if the compiler were only allowed to
      "know" things that have been defined "above" where they are used.

      (Also, C++'s core language parses after the preprocessor commands, so the
      location of a declaration "in a header file" is irrelevant.)

      As a rubric, think of the definition of a class (the {} part) as declaring a
      classes "size", and think of the definition only working if the compiler
      knows the size of each component object.

      So, at this line...
      [color=blue]
      > class TextDB
      > {
      > private:
      > TextDB_Version database_versio n;
      > };[/color]

      The compiler is trying to determine the size of TextDB. But it does not yet
      know the size of TextDB_Version. So it can't define TextDB in terms of
      TextDB_Version, which is not defined yet.

      The solution is to put TextDB_Version above TextDB. C++'s object
      construction rules take precedence - slightly - over any grandiose ideals of
      perfectly encapsulated objects.

      --
      Phlip



      Comment

      • Jonathan Mcdougall

        #4
        Re: Class forward declaration

        >I have the definitions of classes A and B in a header file. Class A has a[color=blue]
        >private member of type B. Class B is defined after class A in the header
        >file. I place a forward declaration of class B above class A but VS.NET
        >still complains with:[/color]

        For the compiler to know the exact size of a class, it must know the
        exact size of its members. By putting only a declaration, the
        compiler does not know the size of the class, hence the error.
        [color=blue]
        >error C2079: 'TextDB::TextDB ::database_vers ion' uses undefined class
        >'TextDB::TextD B_Version'
        >
        >namespace TextDB
        >{
        > class TextDB_Version;
        >
        > class TextDB
        > {
        > private:
        > TextDB_Version database_versio n;
        > };
        >
        > class TextDB_Version
        > {
        > [...]
        > };
        >
        >} // namespace TextDB
        >
        >Is this because this is in a header file? I'm confused.[/color]

        Take a look :

        class A
        {
        int i;
        };

        Here the compiler knows 'A' will have the size
        sizeof(int) + implementation_ defined_things

        In your class :

        class TextDB
        {
        private:
        TextDB_Version database_versio n;
        };

        the compiler tries the same thing :
        sizeof(TextDB_V ersion) + implementation_ defined_things

        and just cannot find the size of TextDB_Version since you only
        declared it.

        You have got two choices :

        1) Define the class TextDB_Version *before* TextDB

        2) Make database_versio n a pointer and allocate the
        memory dynamically


        Jonathan

        Comment

        • Kevin Grigorenko

          #5
          Re: Class forward declaration

          "Jonathan Mcdougall" <DELjonathanmcd ougall@yahoo.ca > wrote in message
          news:7miamvs2ne l97g96r17sccjc1 abeivs2p0@4ax.c om...[color=blue][color=green]
          > >I have the definitions of classes A and B in a header file. Class A has[/color][/color]
          a[color=blue][color=green]
          > >private member of type B. Class B is defined after class A in the header
          > >file. I place a forward declaration of class B above class A but VS.NET
          > >still complains with:[/color]
          >
          > For the compiler to know the exact size of a class, it must know the
          > exact size of its members. By putting only a declaration, the
          > compiler does not know the size of the class, hence the error.
          >[color=green]
          > >error C2079: 'TextDB::TextDB ::database_vers ion' uses undefined class
          > >'TextDB::TextD B_Version'
          > >
          > >namespace TextDB
          > >{
          > > class TextDB_Version;
          > >
          > > class TextDB
          > > {
          > > private:
          > > TextDB_Version database_versio n;
          > > };
          > >
          > > class TextDB_Version
          > > {
          > > [...]
          > > };
          > >
          > >} // namespace TextDB
          > >
          > >Is this because this is in a header file? I'm confused.[/color]
          >
          > Take a look :
          >
          > class A
          > {
          > int i;
          > };
          >
          > Here the compiler knows 'A' will have the size
          > sizeof(int) + implementation_ defined_things
          >
          > In your class :
          >
          > class TextDB
          > {
          > private:
          > TextDB_Version database_versio n;
          > };
          >
          > the compiler tries the same thing :
          > sizeof(TextDB_V ersion) + implementation_ defined_things
          >
          > and just cannot find the size of TextDB_Version since you only
          > declared it.
          >
          > You have got two choices :
          >
          > 1) Define the class TextDB_Version *before* TextDB
          >
          > 2) Make database_versio n a pointer and allocate the
          > memory dynamically
          >
          >
          > Jonathan[/color]

          Thanks a lot, that cleared things up clearly.

          Kevin Grigorenko


          Comment

          • jeffc

            #6
            Re: Class forward declaration


            "Kevin Grigorenko" <kzg110@psu.edu > wrote in message
            news:bk4eo0$1ct k$1@f04n12.cac. psu.edu...[color=blue]
            >
            > Thanks a lot, that cleared things up clearly.[/color]

            As opposed to clearing unclearly? :-)


            Comment

            • Kevin Grigorenko

              #7
              Re: Class forward declaration

              "jeffc" <nobody@nowhere .com> wrote in message
              news:3f65d848_4 @news1.prserv.n et...[color=blue]
              >
              > "Kevin Grigorenko" <kzg110@psu.edu > wrote in message
              > news:bk4eo0$1ct k$1@f04n12.cac. psu.edu...[color=green]
              > >
              > > Thanks a lot, that cleared things up clearly.[/color]
              >
              > As opposed to clearing unclearly? :-)
              >
              >[/color]

              Haha, touché!


              Comment

              Working...