Infinitely looping inclusion!

This is a discussion on Infinitely looping inclusion! within the C++ Programming forums, part of the General Programming Boards category; Hi, If I have 2 classes, each containing a pointer to the other, is there any way I can have ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    Infinitely looping inclusion!

    Hi,

    If I have 2 classes, each containing a pointer to the other, is there any way I can have them in two separate files? For example,

    Code:
    //Main.cpp
    
    #include <stdio.h>
    
    #include "Header1.hpp"
    
    int main()
    {
     Class1 c1;
     return 0;
    }
    Code:
    //Header1.hpp
    
    #ifndef HEADER1_HPP
    #define HEADER1_HPP
    
    #include "Header2.hpp"
    
    class Class1 {
    private:
    Class2 *c;
    public:
    int i;
    int f1() { return c->i; }
    };
    
    #endif

    Code:
    //Header2.hpp
    
    #ifndef HEADER2_HPP
    #define HEADER2_HPP
    
    #include "Header1.hpp"
    
    class Class2 {
    private:
    Class1 *c;
    public:
    int i;
    int f2() {return c->i;}
    };
    
    #endif
    If I compile this, I get a parse error on the line which defines the pointer to the class, which tells me that the compiler thinks it hasn't been defined yet. The compiler is trying to compile the main file. It includes Header1, which includes Header2. This includes Header1 again but because it's protected, nothing happens. Then it gets to the definition of Class2 in Header2, but Class1 is not yet defined in Header1 (it is still processing the line '#include "Header2.hp"' in Header1), so there is an error.

    Any way to avoid this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you are only declaring a pointer to Class2 in Class1, you don't need to #include "Header2.hpp" in Header1.hpp. Just use a forward declaration instead:
    Code:
    class Class2;
    The same is true for the declaration of Class1 in Class2.

    However, since you use the pointers in the inline functions f1() and f2(), it does require the actual header to be included. To solve the issue, you must define either f1() or f2() in a source file and not inside the class definition. Whichever one you choose to move (or both) means that you can use the forward declaration in that header file, removing the circular include.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by Daved
    If you are only declaring a pointer to Class2 in Class1, you don't need to #include "Header2.hpp" in Header1.hpp. Just use a forward declaration instead:
    Code:
    class Class2;
    The same is true for the declaration of Class1 in Class2.

    However, since you use the pointers in the inline functions f1() and f2(), it does require the actual header to be included. To solve the issue, you must define either f1() or f2() in a source file and not inside the class definition. Whichever one you choose to move (or both) means that you can use the forward declaration in that header file, removing the circular include.
    Thanks that's very helpful.

    Now suppose I have a typedef instead. Is there any such thing as a "forward typedef declaration"?

    e.g.

    Code:
    //Main.cpp
    
    #include <stdio.h>
    
    #include "Header1.hpp"
    
    int main()
    {
     Class1 c1;
     return 0;
    }
    Code:
    //Header1.hpp
    
    #ifndef HEADER1_HPP
    #define HEADER1_HPP
    
    #include "Header2.hpp"
    
    class Class1;
    
    typedef ClassArray *Class1;
    
    class Class1 {
    private:
    Class2 *c;
    public:
    int i;
    int f1() { return c->i; }
    };
    
    #endif

    Code:
    //Header2.hpp
    
    #ifndef HEADER2_HPP
    #define HEADER2_HPP
    
    #include "Header1.hpp"
    
    class Class2 {
    private:
    ClassArray *c;
    public:
    int i;
    int f2() {return c->i;}
    };
    
    #endif
    Ignore the functions, even comment them out if you like. They're not my main concern.

    Is there anyway to forward-declare the typedef in Header2, like you did with the class?

    Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Not that I know of, but you might be able to put the typedef in a third header file included by both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM

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