我有以下类结构。这显然不会编译。我可以转发声明B.然后,我可以在函数调用中使用函数指针,但它不是一个很好的解决方案,因为我会从A :: funcA调用A中的其他函数或将B的声明的一部分放入标题中,这将是几百行并且是实用的。
处理这种情况的首选方式是什么(其他)?
class B;
class A
{
public:
void funcA(B* b);
double funcA2();
int funcA4(B* b);
private:
E memberA1;
E memberA2;
};
void A::funcA( B* b) {
b->funcB(a->memberA1, a->memberA2);
class B : public BBase
{
public:
void FuncB(E* e1, E* e2)
{
/* using const objects of B that are initialized
by B() and some other functions... */
}
std::vector<C*> memberB1; // C has std::vector<A*> memberC1
};
int main() {
calling B->memberB1.at(0)->memberC1.at(0)->funcA();
}
我有以下内容(省略一些不必要的行):
A.H
Class B;
Class A {
declaration of A
};
A.cpp ....
B.h
#include "A.h"
#include "BBase.h"
Class B {
declaration of B
};
B.cpp ....
BBase.h
#include "C.h"
#include "A.h"
#include "AInterface.h"
typedef std::vector<AInterface> AList;
BBase {
declaration of abstract BBase
};
BBase.cpp ....`
但我仍然得到错误:成员访问不完整类型'B'。
答案 0 :(得分:1)
假设E
和C
被充分声明/定义,那么你拥有的几乎就好了。问题是在定义A
类之前定义B
的成员函数,确保完全定义了类B
(实际的类,而不是完整的实现)在你实现A
成员函数之前,它的成员函数。
这样的事情:
class B;
class A { ...; void member_function(B*); ... };
class B { ...; void other_member_function(); ... };
void A::member_function(B* b) { ...; b->other_member_function(); ... }
答案 1 :(得分:0)
我建议使用Interface而不是具体的B类
class B : public IB{
};
并将IB而不是B传递给A.
要么在c ++中更好地使用模板函数并绑定你想要调用的成员函数
a.funcA( std::bind( &B::funcB, &b, someArg );