Thread: including headers inside headers

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18

    Question including headers inside headers

    Hi all,

    I have a header file "realTreeNode.h" and starts like this;

    Code:
    #ifndef REALTREENODE_H
    #define REALTREENODE_H
    
    #include "keyedItem.h"
    
    #include 
    using namespace std;
    
    typedef KeyedItem TreeItemType;
    
    const int MAX_DIFF_PAGE_NUMBERS = 50;
    
    typedef void (*FunctionType)(TreeItemType& anItem, TreeItemType pageNumbers[MAX_DIFF_PAGE_NUMBERS]);
    
    class TreeNode // a node in the tree 
    {
    ....
    };

    In addition to this I have another header file "realBST.h" and starts like this;

    Code:
    #ifndef REALBST_H
    #define REALBST_H
    
    #include "realTreeException.h"
    #include "realTreeNode.h"
    
    typedef void (*FunctionType)(TreeItemType& anItem, TreeItemType pageNumbers[MAX_DIFF_PAGE_NUMBERS]);
    
    class BinarySearchTree
    {
    ...
    };
    The thing I want to do here is;

    Creating a private BinarySearchTree object ( variable / property ) in TreeNode class.

    However, when I add #include "realBST.h"in order to create a private BinarySearchTree member for TreeNode to "realTreeNode.h" file, it gives many irrelavent errors.

    How could I solve this problem, any suggestions, any opinions would be helpful.

    Best wishes,
    Sarp Arda Coskun
    www.bilgiciftligi.com

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Ok, if I am understanding you right you have a header file named realtreenode.h and another header named RealBST.h and you want to include realBST.h in in realtreenode.h and I am noticing that realBST.h already includes realtreenode. I won't go into infinite includes because I see you have the inclusion guards, but your problem is still realated.

    The short answer is: two objects cannot mutually contain each other.

    In other words if you have two classes Foo and Bar:

    Code:
    class Bar;// forward declaration
    
    class Foo
    {
    	Bar mBar; // error - object not defined - we only have a declaration
    	Bar* mptBar; // ok - this is the work around - you have to use pointers
    };
    
    class Bar
    
    {
    	Foo mFoo; // ok
    };
    If you need more explanation let me know.
    Last edited by Darryl; 04-17-2005 at 01:33 PM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18
    Hi Darryl,

    I got what you mean and it is a brilliant idea.

    However, I cannot add the #include "realBST.h" into "realTreeNode.h" and therefore, I cannot even create a pointer to BinarySearchTree class. Since, realTreeNode header file do not know the BinarySearchTree class unless I add "realBST.h" as you know friend.

    What could I do?

    If you wish I can send the code to you.

    Thanks,
    Sarp Arda Coskun
    www.bilgiciftligi.com

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18

    Thumbs up Bingoo!

    I got it now, it works.

    First declaration worked. I didn't include the header file, just declaration and a pointer did everything working well.

    Thank you Darryl.
    Sarp Arda Coskun
    www.bilgiciftligi.com

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > using namespace std;
    Don't put namespaces inside header files.
    People including your header file might not want the namespace being polluted in this way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Salem
    > using namespace std;
    Don't put namespaces inside header files.
    People including your header file might not want the namespace being polluted in this way.
    Agreed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including extra .cpp files visual2008
    By Cathalo in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2009, 03:29 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. including headers and forward declaration
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2007, 02:47 PM
  4. Struct inside a Struct inside a Struct?!?
    By tegwin in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 11:50 AM
  5. Pointer to class itself
    By Cdumbie in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2002, 12:24 PM