home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!utcsri!torn!thunder!bkssmith
- From: bkssmith@thunder.LakeheadU.Ca (BKS Smith)
- Newsgroups: comp.lang.c
- Subject: Re: Please help me with my #include mess-up.
- Message-ID: <519@thunder.LakeheadU.Ca>
- Date: 12 Nov 1992 05:39:25 GMT
- References: <1992Nov11.172840.5540@news.ysu.edu>
- Organization: Lakehead University, Thunder Bay, Ont., Canada
- Lines: 58
-
- In article <1992Nov11.172840.5540@news.ysu.edu> ae007@yfn.ysu.edu (Daniel Newcombe) writes:
- >
- >Well here is my problem: I have these modules:
- > MOSTYPES.H - defines several types I am using in this
- > project, such as boolean, and a few structs and typedefs
- >QUEUE.H - the header file for my queue class which has a few
- > inline things in it.
- > This has to include mostypes.h because it needs boolean.
- >QUEUE.CPP - the rest of the code for the queues. It includes
- > queue.h
- >MAINMOD.CPP - my main module. It includes QUEUE.H and MOSTYPES.H.
- >When I compile I get a whole mess of errors saying that all sorts
- >of things in MOSTYPES.H are redefined. There are a lot more
- >modules that are going to have to use mostypes.h before it's
- >all over with.
- >
- >My question - how do I get this to compile, while still keeping
- >the code portable and re-usable???
- >
-
- The problem is that you are actually #including MOSTYPES.H _twice_.
- MAINMOD.CPP #includes QUEUE.H which #includes MOSTYPES.H, then it tries to
- #include MOSTYPES.H again. A C++ compiler will choke on this.
- The solution is pretty simple. Change MOSTYPES.H to this:
-
- // MOSTYPES.H
-
- #ifndef MOSTYPES_INCLUDED // use whatever name you want here
- #define MOSTYPES_INCLUDED 1 // as long as it matches this one
- .
- .
- .
- [put your original code for MOSTYPES.H here]
- .
- .
- .
- #endif
- // end of MOSTYPES.H
-
- Now when you try to include it the second time, the #ifndef statement is
- false, so the body of your original code is not #included.
- BTW, to be truly portable, _every_ header file you create should have this
- safeguard.
-
- BKS
-
- PS. this really should have been posted to comp.lang.c++...
-
-
- -----------------------------------------------------------------------------
- |Scotty:Captain, we din' can reference it! | |
- |Kirk: Analysis, Mr. Spock? | |
- |Spock: Captain, it doesn't appear in the symbol table.|bkssmith@ |
- |Kirk: Then it's of external origin? |thunder.LakeheadU.Ca|
- |Spock: Affirmative. | |
- |Kirk: Mr. Sulu, go to pass two. | |
- |Sulu: Aye aye, sir, going to pass two. | |
- -----------------------------------------------------------------------------
-