#include?

This is a discussion on #include? within the Windows Programming forums, part of the Platform Specific Boards category; What can I use instead of #include if I want all my units to be able to use each other... ...

  1. #1
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22

    #include?

    What can I use instead of #include if I want all my units to be able to use each other... cuz now it displays "Include files nested too deep" error :S

    thx!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Max include nesting level is compiler specific. One thing you can do is make sure you have a protection on your header files so they can only be included once. Either one will work fine:
    Code:
    // works on VC++ and some other compilers
    #pragma once
    
    // ... your header source here ...
    Code:
    // will work anywhere. just make sure to replace the name of the header file
    #ifndef _NAMEOFHEADER_H
    #define _NAMEOFHEADER_H
    
    // ... your header source here ...
    
    #endif

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Also, make your own headers #include as few other headers as possible. If you don't need a complete type, don't include the header, just forward declare the classes.

    For example, this would be unnecessary:

    Code:
    A.H
    #ifndef __A_H_INCLUDED_____
    #define __A_H_INCLUDED_____
    #include "b.h" // assume this defines class B
    
    class A{
    private:
       B& myBObject;
       B* myBPtr;
    public:
       explicit A(B&);
       void otherFunction(B*);
       B f2();
       void f3(B);
    };
    
    #endif
    Here, you will notice that in the above code, it was NOT necessary for B to be a complete type. B is only used as a reference (B&), a pointer (B*), or a parameter/return type, so it can legally still be an incomplete type (a type that has been declared but not defined). There are no member variables of type B (only type B& or B*), and B is not a base class of A. Further, we have no containers of B that might need to know its definition. So instead of defining B by the include, we declare it instead:

    Code:
    A.H
    #ifndef __A_H_INCLUDED_____
    #define __A_H_INCLUDED_____
    class B; //declares class B
    
    class A/*A is declared here*/{
    private:
       B& myBObject;
       B* myBPtr;
    public:
       explicit A(B&);
       void otherFunction(B*);
       B f2();
       void f3(B);
    }/*A is defined here*/;
    
    #endif
    In the code above, I also show where A is declared and defined. Note A can be declared as often as you like; it should only be defined in one header. Learn all the rules about incomplete types; often only a declaration is needed, not a definition.

    Now, A.CPP will certainly need to include B.H (A.CPP will need B to be a complete type, because it will want to use member functions or data of B, and it must know how to copy B objects). But the goal is to make your headers (.h) files include as few other headers as possible; put your includes in the .CPP files only when possible.

    Never #include another header of yours unless you actually need a complete type. For an incomplete type, just forward declare it.

    You can also use tricks like the PIMPL idiom to hide your class's "private parts", which will probably cut down drastically on the need for full definitions of classes.
    Last edited by Cat; 06-25-2003 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  2. Socket programming
    By kahad in forum C Programming
    Replies: 3
    Last Post: 12-14-2006, 04:37 PM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21