CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2001
    Posts
    391

    #Include not working for me...

    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.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    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

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    '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...

  5. #5
    Join Date
    Dec 2002
    Posts
    287
    Is it WIN32 code ? If yes, then you don't even have to typedef DWORD. Just include <windows.h>/<winbase.h>.

    Dan

  6. #6
    Join Date
    Dec 2001
    Posts
    391
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured