Thread: nested #includes

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    nested #includes

    I have two classes class a and class b
    I #include class a in class b anb class b in class a the reason for this is that class a needs to interact with class b and vice versa. I have my #include guards in but I still get the error message #include nested too deeply. Is there a way to get around this.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Give us your code.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Code:
    #include "classb.h"
    
    #ifndef CLASS_A
    #define CLASS_A
    
    class A{
    
    public:
                .
                .
                .
    const string A_Attacks_B(classBobject&)
    
    private:
                .
                .
    };
    
    
    
    #include "classa.h"
    
    #ifndef CLASS_B
    #define CLASS_B
    
    
    class A{
    
    public:
                .
                .
                .
    const string B_Attacks_A(classAobject&)
    
    private:
                .
                .
    };

    This is the basis of my code then in main I call each function

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Is that code all inside one file?

    Anyways the type of relationship between classA and classB expressed in the member functions, const string A_Attacks_B(classBobject&) and const string B_Attacks_A(classAobject&) doesn't call for an include. A simple forward declaration will be enough.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In fact, you should use a forward declaration for both files, since you should prefer a forward declaration over including a header.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Also, on both classes include stataments should go after the header guards and that is the main reason why you are having errors.

    Secondly I blieve you second class sould be B, not A.
    Code:
    #ifndef CLASS_B
    #define CLASS_B
    
    
    class B{
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Thanks all that worked but now I am getting an error message that classA has not been declared

    Code:
    #ifndef
    #define
    
    #include "classb.h"
    
    class A{
    
    public:
                .
                .
                .
    const string A_Attacks_B(classBobject&)
    
    private:
                .
                .
    };
    
    
    #ifndef
    #define
    
    #include "classa.h"
    
    class B{
    
    public:
                .
                .
                .
    const string B_Attacks_A(classAobject&)
    
    private:
                .
                .
    };
    the code is seperate header files

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    also I did use a forward declaration but I got errors about invalid use of a struct classA or invalid use of struct classB

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > also I did use a forward declaration but I got errors about invalid use of a struct classA or invalid use of struct classB

    It's possible that you didn't tell us the whole story and the bits of code we aren't seeing make other uses of those classes. How about showing us the whole of the class definition? Or is it too big?

    > Thanks all that worked but now I am getting an error message that classA has not been declared

    What happened to the header guards?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    No there are no other functions but that one that uses it it is the only instance i have of that object

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should remove #include "classb.h" and put
    Code:
    class B;
    You should do the same for class A. If you get errors doing that post your attempt and the exact errors.

    If you used struct instead of class, the compiler might complain because you use struct in one place and class in another.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM