cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/14/17:00:28

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: recursive structures?
Date: Sat, 14 Nov 1998 13:38:24 -0800
Organization: Alcyone Systems
Lines: 52
Message-ID: <364DF850.2629CE74@alcyone.com>
References: <364DD66B DOT 3CB7DAD1 AT nospam DOT student DOT tue DOT nl>
NNTP-Posting-Host: charmaine.alcyone.com
Mime-Version: 1.0
X-Mailer: Mozilla 3.01 (X11; I; Linux 2.0.34 i686)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Wouter Bijlsma wrote:

> How can I define a recursive structure without getting parse errors or
> 'incomplete data type' errors?
> 
> typedef struct {
>                         unsigned     Tag;
>                         Plane        *RootPlane;
>                         BSPNode  *FrontNode,BackNode;
>                         Polygon     *Polygons;
>                     }  BSPNode;
> 
> This does *not* work....

You are defining an anonymous struct, and then typedef'ing it tp
BSPNode.  The problem is, by the time the BSPNode * members are
encountered by the compiler, BSPNode is an incomplete type, because it
has not yet been defined (it is defined after the struct is defined).

There are two solutions:

Create the typedef first, then the struct:

    typedef struct S S;

    struct S {
        ...
        S *s;
    };

or you can refer to the structure itself with a tag, rather than with
the typedef:

    typedef struct S {
        ...
        struct S *s;
    };

Note that in either case you must give the struct a tag (which you are
not doing above).

In C++ these kinds of things do not cause a problem since reference to
structs by tag is no longer necessary.

-- 
Erik Max Francis / email max AT alcyone DOT com / whois mf303 / icq 16063900
Alcyone Systems / irc maxxon (efnet) / finger max AT sade DOT alcyone DOT com
  San Jose, CA / languages En, Eo / web http://www.alcyone.com/max/
          USA / icbm 37 20 07 N 121 53 38 W / &tSftDotIotE
             \
            / The pure and simple truth is rarely pure and never simple.
           / Oscar Wilde

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019