Click to See Complete Forum and Search --> : #Include not working for me...


quantass
January 20th, 2003, 04:55 AM
I'm having some problems with #include and my header files. I have one header file with the following contents:
---(MyHeader1.h)---

#if !defined(__MyHeader1__)
#define __MyHeader1__

#Include "MyHeader2.h"

class CMyHeader
{
DWORD myfunction(void);
}

#endif

---(MyHeader2.h)---

#if !defined(__MyHeader2__)
#define __MyHeader2__

#include "MyHeader1.h"

typedef unsigned long DWORD;

#endif
---

As you can see MyHeader1.h makes reference to a DWORD type which is defined within MyHeader2.h. Myheader2.h is included but also has the directive to include MyHeader1.h however that file has already #DEFINED its special name and so shall not be included again. What I expect then is for MyHeader2.h to be traversed the rest of the way adding the typedef DWORD and placing all of this info into MyHeader1.h which provides it the necessary info (the DWORD definition) to declare itself.

What I'm experiencing is the typedef DWORD line in MyHeader2.h is never being defined for MyHeader1.h and so I keepreceving a "syntax error: identifier DWORD".

Can someone please explain to me why #include isnt working the way I expect it to.

PaulWendt
January 20th, 2003, 06:11 AM
Either put the typedef for DWORD into a third #include file and
have both headers include that file ... or put the typedef before
the #include "header1" inside of header2. The former option is
probably the more desirable of the two. Chances are that you'll
have other "global" things like DWORD that you might want to use
everywhere ... so it'd behoove you to put all of these really
common things inside one header file.

--Paul

KevinHall
January 20th, 2003, 08:10 AM
You'll see the problems you describe if in one of your sources you include MyHeader2.h before MyHeader1.h. If you include MyHeader1.h before MyHeader2.h, then I don't believe you'd have the problem.

That being said however, your design is poor. Personally, I believe any design that forces users to include files in a particular order should be avoided if possible. I strongly suggest you listen to the suggestions Paul laid out.

- Kevin

Andreas Masur
January 20th, 2003, 10:22 AM
'include' is working fine...this is a typical problem of having circular includes...and inclusion guards will not help preventing this.

Basically you need to do what the others already said...my question would be...is there a need to include 'MyHeader1.h' in the second header? I only see the typedef but I assume you just shortened your code before posting...

DanM
January 20th, 2003, 10:46 AM
Is it WIN32 code ? If yes, then you don't even have to typedef DWORD. Just include <windows.h>/<winbase.h>.

Dan

quantass
January 20th, 2003, 01:42 PM
Yupper, that fixed it. Thanks for the terrific replies. And yes, I'm making a Win32 DLL -- I will use Winbase.h as you suggested DanM.