[comp.sys.mac.programmer] Think C #include Question

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (03/01/91)

I am working with 3 classes (call them Foo, Bar, and Baz).  Foo and
Bar are subclasses of Baz.  There header files are as follows:

-----------Foo.h-----------
#define _H_Foo   /* prevents file from being included more than once */

#include Baz.h  /* superclass header */
#include Bar.h  /* other file's header */

struct Foo : Baz
{
    Bar *myBar; /* an object of the Bar class */
       .
       .
};

----------Bar.h------------
#define _H_Bar

#include Baz.h
#include Foo.h

struct Bar : Baz
{
    Foo *myFoo; /* an object of the Foo class */
       .
       .
};
---------------------------

The problem is that I want each Foo to know about a Bar, and I want
each Bar to know about a Foo.  Unfortunately I get a syntax error when
I try to compile.  When compiling Bar.h, it first has to compile
Foo.h.  When Foo.h tries to declare myBar, it can't because the Bar
class has not been; thus I get a syntax error.

How can I get around this problem?

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

olson@bootsie.uucp (Eric K. Olson) (03/01/91)

In article <1991Feb28.223430.24645@ux1.cso.uiuc.edu> dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:
>
>-----------Foo.h-----------
>#define _H_Foo   /* prevents file from being included more than once */
>
>#include Baz.h  /* superclass header */
>#include Bar.h  /* other file's header */
>
>struct Foo : Baz
>{
>    Bar *myBar; /* an object of the Bar class */
>       .
>       .
>};
>
>----------Bar.h------------
>#define _H_Bar
>
>#include Baz.h
>#include Foo.h

Leave this #include out

>
>struct Bar : Baz
>{
>    Foo *myFoo; /* an object of the Foo class */

change this to:
     struct Foo *myFoo;

>       .
>       .
>};
>---------------------------

This works equivalently, but does not require the Foo.h header file.

For another example, see CPanorama, which needs to use CScrollPane,
which already #included CPanorama.

-Eric



-- 
Eric K. Olson, Editor, Prepare()      NOTE:     olson@bootsie.uucp doesn't work
Lexington Software Design             Internet: olson@endor.harvard.edu
72A Lowell St., Lexington, MA  02173  Uucp:     harvard!endor!olson
(617) 863-9624                        Bitnet:   OLSON@HARVARD

rac@macro.co.jp (Robert Coie) (03/04/91)

In article <1991Feb28.223430.24645@ux1.cso.uiuc.edu>,
dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:

>> I am working with 3 classes (call them Foo, Bar, and Baz).  Foo and
>> Bar are subclasses of Baz.  There header files are as follows:
>>
>> [header files containing mutually recursive Think C classes]
>>
>> The problem is that I want each Foo to know about a Bar, and I want
>> each Bar to know about a Foo.  Unfortunately I get a syntax error when
>> I try to compile.  When compiling Bar.h, it first has to compile
>> Foo.h.  When Foo.h tries to declare myBar, it can't because the Bar
>> class has not been; thus I get a syntax error.
>>
>> How can I get around this problem?

When you define a new class with struct Foo : Baz{}, Think C
automatically generates a typedef struct Foo{} Foo; statement for you,
enabling you to use the name Foo when referring to instances of this class.
I think that the following two structures are similar enough to your
Foo and Bar classes to illustrate the problem:

typedef struct foo {
	bar *theBar;
} foo;
typedef struct bar {
	foo *theFoo;
} bar;

As in your example, this will generate a syntax error.  Try rewriting
it without relying on the typedef:

typedef struct foo {
	struct bar *theBar;
} foo;
typedef struct bar {
	struct foo *theFoo;
} bar;

For more information on this, check the sources for CScrollPane and
CPanorama, which are also mutually recursive, or look in your favorite
C book about structure tags and incomplete types.

--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Robert Coie             Love is a vast exaggeration of the difference
 rac@macro.co.jp         between one person and everybody else. - GBS