CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Multi include

  1. #1
    Guest

    Multi include

    Hi I have a little include problem. I will try to explain it as good as I can. Here is some code from a class:

    // test.h

    #ifndef .....
    #define ...

    #include <macrodef.h>
    #include <stuff.h>

    class test {
    public:
    ...
    private:
    SOME_STRUCT *struct;
    };

    #endif




    SOME_STRUCT is defined in stuff.h and therefore stuff.h is needed in this file.
    The thing is the code to this is placed in "test.cpp" which is including "test.h". So far so good. The problem is that "stuff.h" is defining a macro of a macro defined in "macrodef.h". That new macro is needed in "test.cpp". If I compile this VC++ complins anut that this macro is aldready defined in the start file wich includes "test.h".
    It seems that "macrodef.h" must only be included once. Sice the macro is only used in "test.cpp" I would like to include it in there but I must include "macrodef.h" before "stuff.h" and I need that file in "test.h".
    Do you understand my problem? Is there a good way to solve this?

    Excuse me for my language..

    / Anders


  2. #2
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: Multi include

    I'm not too clear on what your problem is but I have two recommendations:

    1. You only need to include a header file if the compiler needs specific information about the class or struct your using. In the case of test.h, since you're declaring just a pointer to SOME_STRUCT, then all the compiler needs to know is that it's a struct. The compiler doesn't care about its size or its members. Just telling it it's a struct is enough, so just "forward" declare it at the top of test.h like this:

    struct SOME_STRUCT;

    Now you don't have to include stuff.h in your test.h, but you will need it inside test.cpp.

    2. You should make your header files as self-contained as possible. In other words, if stuff.h requires something that's defined in macrodef.h then include macrodef.h inside stuff.h. This allows anyone who needs to use stuff.h to not have to worry about other header files that stuff.h needs.

    I hope to have helped. Good luck!
    Alvaro



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