Topic: Circular definition problem. Please HELP!


Author: jhcg8669@uxa.cso.uiuc.edu (Jack H Choquette)
Date: 25 Jan 92 05:13:55 GMT
Raw View

I have a circular definition problem with a C++ program I'm writing.  I
won't explain the specifics of my program or why I need to do it, but
basically I need to do this:

 class foo
 {
   foo2 window[50];
   ...etc...
 }

 class foo2
 {
   foo* foo_ptr;
   ...etc...
 }

I run into a circular definition problem when I try to compile this.  In
order to define foo, foo2 needs to be defined.  But in order to define
foo2, foo needs to be defined.  Is there a way to get the compiler to
accept the circual definition?  Don't tell me just to not do it, I can't
avoid it.  My code should work quite well if I can do this.  Can anyone
help me?

/jack

PS  If you want to see an expanded example of how I use this, I'll be
    glad to do so.




Author: pew@cs.brown.edu (Peter E. Wagner)
Date: 25 Jan 92 19:56:52 GMT
Raw View
In article <1992Jan25.051355.8437@ux1.cso.uiuc.edu>, jhcg8669@uxa.cso.uiuc.edu (Jack H Choquette) writes:
|>
|>
|> I have a circular definition problem with a C++ program I'm writing.  I
|> won't explain the specifics of my program or why I need to do it, but
|> basically I need to do this:
|>
|>  class foo
|>  {
|>    foo2 window[50];
|>    ...etc...
|>  }
|>
|>  class foo2
|>  {
|>    foo* foo_ptr;
|>    ...etc...
|>  }
|>
|> I run into a circular definition problem when I try to compile this.  In
|> order to define foo, foo2 needs to be defined.  But in order to define
|> foo2, foo needs to be defined.  Is there a way to get the compiler to
|> accept the circual definition?  Don't tell me just to not do it, I can't
|> avoid it.  My code should work quite well if I can do this.  Can anyone
|> help me?
|>

I just posted some bad info.  I said you could simply do:

     class foo2;

  class foo
  {
    foo2 window[50];
    ...etc...
  }

  class foo2
  {
    foo* foo_ptr;
    ...etc...
  }

But I wasn't paying attention.  class foo contains an instance of
foo2, not just a pointer to a foo2 object.  The declaration of foo2

    class foo2;

only works if you only need a pointer.  If an instance is declared,
the compiler needs to know the definition of the class so it can be
constructed.  At any rate, the above problem can be solved by
reversing things and using the same strategy, since class foo2 only
contains a pointer to an object of type foo.

     class foo;

  class foo2
  {
    foo* foo_ptr;
    ...etc...
  }

  class foo
  {
    foo2 window[50];
    ...etc...
  }


Sorry about the misinformation.

    Peter
--
----------------------------------------------------------------
Peter E. Wagner          (401)863-7685        pew@cs.brown.edu
Department Computer Science   Box 1910        pew@BROWNCS.BITNET
Brown University, Providence, RI 02912        uunet!brunix!pew
----------------------------------------------------------------