Thread: Recursive Includes

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Recursive Includes

    Hello, I'm trying to implement a sort of callback object, but having some trouble with a recursive include problem, to demonstrate:

    Code:
    #ifndef TEST1_HPP_
    #define TEST1_HPP_
    
    #include "test2.hpp"
    
    class A {
    	B *b;
    	
    	public:
    		A(B *obj) {
    			b = obj;
    		}
    		
    		void func() {
    			b->print();
    		}
    };
    
    #endif /*TEST1_HPP_*/
    Code:
    #ifndef TEST2_HPP_
    #define TEST2_HPP_
    
    #include "test1.hpp"
    
    class B {
    	private:
    		A a;
    	
    	public:
    		B() : a(this) {}
    		
    		void test() {
    			a.func();
    		}
    		
    		void print() {
    			std::cout << "Hello World!" << std::endl;
    		}
    };
    
    #endif /*TEST2_HPP_*/
    Code:
    #include "test1.hpp"
    
    int main() {
    
    	return 0;
    }
    EDIT: The errors in question are:
    Code:
    In file included from test2.hpp:4,
                     from test.cpp:3:
    test1.hpp:7: error: ISO C++ forbids declaration of ‘B’ with no type
    test1.hpp:7: error: expected ‘;’ before ‘*’ token
    test1.hpp:10: error: expected `)' before ‘*’ token
    test1.hpp: In member function ‘void A::func()’:
    test1.hpp:15: error: ‘b’ was not declared in this scope
    In file included from test.cpp:3:
    test2.hpp: In constructor ‘B::B()’:
    test2.hpp:11: error: no matching function for call to ‘A::A(B* const)’
    test1.hpp:6: note: candidates are: A::A()
    test1.hpp:6: note:                 A::A(const A&)
    The obvious question is, what am I doing wrong in trying to get this to work?
    Last edited by nempo; 08-12-2008 at 01:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't know what IDE you're using, but you should be able to get the output of just the pre-processor (before compilation even starts). You'll see that one of your classes is defined before the other, so calling B in the class A definition won't work, since B hasn't happened yet.

    I think you'll need a forward declaration (like "class B;") so that the name B is recognized as a type.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The definition of class A doesn't need the definition of class B. Therefore, don't include test2.hpp, but instead just declare the class:
    Code:
    class B;
    
    class A { ... };
    However, the definitions of A's members do need the definition of B, so separate them out into the implementing source file.
    Code:
    #include "test1.hpp"
    #include "test2.hpp"
    
    void A::func()
    {
      b->print();
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Cheers!

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. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM