We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,939 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem with recursive includes

I have a problem with recursive imports in C++. I have this 2 classes:

//File A.h
#include "B.h"

class A{
        public:		 
		A(){};

		void doA(B b){ b.doB(); }
}
//File B.h
#include "A.h"

class B{
	public:		 
		B(){};

		void doB(A a){ a.doA(); }
}

I tried to use forward declarations but it doesn't work because i call methods "doA" and "doB" so I cant use forward declarations.

Do someone know what can I do to compile it?

Thank you!

4
Contributors
6
Replies
5 Months
Discussion Span
2 Years Ago
Last Updated
7
Views
Question
Answered
garea
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Are you macro protecting your header files?

#ifndef _SOME_HEADER
#define _SOME_HEADER
class SomeClass{...};
#endif

or

#pragma once
class SomeClass{...};
dusktreader
Posting Whiz in Training
259 posts since Jan 2010
Reputation Points: 152
Solved Threads: 42
Skill Endorsements: 0

Yes, I have

#ifndef _SOME_HEADER
#define _SOME_HEADER
....
#endif

in all my classes.

What i dont undestand is why i can't find a solution in the web, because i think it's a very common problem....

garea
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What's the compiler error look like?

dusktreader
Posting Whiz in Training
259 posts since Jan 2010
Reputation Points: 152
Solved Threads: 42
Skill Endorsements: 0

the problem is the chicken and the egg paradox. what comes first in your code A or B?. if A is first and A uses B then the compiler goes to B to find out what B is but B has A in it and A isn't finished so it doesn't know what A is yet either. the same thing will happen if B is first. unfortunately there is no solution for this problem. it just isn't possible in c++ right now. you can have A have a B as long as B doesn't have an A in it or the other way around with B but A cant have B when B has A in it. i really hope I'm making sense

NathanOliver
Posting Virtuoso
1,515 posts since Apr 2009
Reputation Points: 281
Solved Threads: 277
Skill Endorsements: 3

Thank you, but i have just solved the problem.
I used forward references in A.h and B.h, and used includes in A.cpp and B.cpp

garea
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Years Ago by dusktreader and NathanOliver

I don't know if this is standard C, but in C++ Builder works.

//File A.h
#include "B.h"

class B;            // pre-declare B class
class A {
public:		 
    A(){};
    void doA(B b){ b.doB(); }
}
//File B.h
#include "A.h"

class A;             // pre-declare A class
class B {
public:		 
    B(){};
    void doB(A a){ a.doA(); }
}
rodc
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0788 seconds using 2.71MB