Recursive imports problem
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!
Re: Recursive imports problem
You can use forward declarations and hide the object implementation behind a pointer.
So A.h would look like
Code:
class B;
class A{
public:
A(){}
void doA(B *b);
};
A.cpp would be:
Code:
#include "A.h"
#include "B.h"
void A::doA(B *b){
b->doB( this );
}
B.h would be:
Code:
class A;
class B{
public:
B(){};
void doB(A* a);
};
and B.cpp would be:
Code:
#include "B.h"
#include "A.h"
void B::doB(A *a){
a->doA( this );
}
Regards.
Re: Recursive imports problem
Thank you very much, it works!!!