【发布时间】:2014-11-25 15:41:21
【问题描述】:
我有以下类结构。这显然不会编译。我可以转发声明 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();
}
我有以下内容(省略一些不必要的行):
啊.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”。
【问题讨论】:
-
你做在
BBase.cpp中包含"B.h"? -
不,在
cpp-s 中只包含对应的.h。例如BBase.cpp仅具有BBase.h。 -
在哪里你得到错误?如果您使用
B,则需要B类的完整定义,因此需要在该源文件中包含B.h文件。 -
我在尝试使用
B->funcB(...);时在A.cpp中得到它@在B.h. -
您能否创建一些完整代码,Minimal, Complete, and Verifiable example,并展示给我们?
标签: c++ composition member-functions