![](/img/trans.png)
[英]error: member access into incomplete type''; note: forward declaration of ''
[英]error: member access into incomplete type : forward declaration of
我在同一个.cpp文件中有两个类:
// forward
class B;
class A {
void doSomething(B * b) {
b->add();
}
};
class B {
void add() {
...
}
};
转发不起作用,我无法编译。
我收到此错误:
error: member access into incomplete type 'B'
note: forward declaration of 'B'
我正在使用clang编译器(clang-500.2.79)。
我不想使用多个文件(.cpp和.hh),我只想在一个.cpp上编码。
我不能在A类之前写B类。
您对如何解决我的问题有任何想法吗?
将doSomething
定义移至其类声明的外部,并移至B
之后,还可以通过将其public
或将其friend
使其add
到A
。
class B;
class A
{
void doSomething(B * b);
};
class B
{
public:
void add() {}
};
void A::doSomething(B * b)
{
b->add();
}
使用该类之前,必须具有B
类的定义 。 否则编译器将如何知道存在诸如B::add
类的功能?
可以在A
类之前定义B
类,或者将A::doSomething
的主体移到B
类定义之后,例如
class B;
class A
{
B* b;
void doSomething();
};
class B
{
A* a;
void add() {}
};
void A::doSomething()
{
b->add();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.