Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!nstn.ns.ca!news.cs.indiana.edu!samsung!uunet!tcs!tixdemo!gwu From: gwu@tixdemo.tcs.com (George Wu) Newsgroups: comp.lang.c++ Subject: Re: Problems maintaining header files Message-ID: <1584@tcs.tcs.com> Date: 23 Jan 91 00:36:58 GMT References: <15917@reed.UUCP> Sender: root@tcs.com Reply-To: gwu@tixdemo.tcs.com (George Wu) Organization: Teknekron Communications Systems Lines: 46 In article <15917@reed.UUCP>, minar@reed.bitnet (Nelson Minar,L08,x640,7776519) writes: |> |> In C, I barely managed to keep all my header files straight and organized. |> C++ just compounds my problems. |> |> main.C is what you would expect. In the process of doing its thing, |> it creates an instance of LineWindow and an instance of Turtle. |> Therefore, it includes the files lineWindow.h and turtle.h |> |> lineWindow.C has to include lineWindow.h, obviously, and turtle.C includes |> turtle.h. |> |> The problem is, one of the member variables in class ScreenTurtle is a |> reference to an instance of LineWindow. Therefore, the header turtle.h |> must also include lineWindow.h for the class declaration. |> |> So, when one goes to compile main.C, it includes lineWindow.h and then |> later includes turtle.h which again includes lineWindow.h. Unless I take |> precautions (either a '#pragma once' or a series of '#ifndef .. #define') |> the header is included twice, and class LineWindow is declared twice, and |> the compiler gets annoyed. If ScreenTurtle has a member variable *pointing* to an instance of a LineWindow, then it doesn't need to include lineWindow.h. You can just add class LineWindow; to lineWindow.h. It's sort of like a function extern declaration for externally defined classes. You also might be able to do this with a reference, but I'm not sure. Try it, and if it doesn't work, switch to a pointer. (Yeah, I know, some people prefer references to explicit pointers.) |> Are constructs like '#pragma once' just entirely common in C++ headers? I |> find them ugly.. We've never used a #pragma, and we've got hundreds of thousands of lines of code. We do use the #ifndef X, #define X, /* declarations */, #endif to surround the contents of all header files, though. George ---- George J Wu | gwu@tcs.com or uunet!tcs!gwu Software Engineer | 2121 Allston Way, Berkeley, CA, 94704 Teknekron Communications Systems, Inc.| (415) 649-3752