LOL*_*KFC 37 c++ forward-declaration
我在同一个.cpp上有两个类:
// forward
class B;
class A {
void doSomething(B * b) {
b->add();
}
};
class B {
void add() {
...
}
};
Run Code Online (Sandbox Code Playgroud)
前进不起作用,我无法编译.
我有这个错误:
error: member access into incomplete type 'B'
note: forward declaration of 'B'
Run Code Online (Sandbox Code Playgroud)
我正在使用clang编译器(clang-500.2.79).
我不想使用多个文件(.cpp和.hh),我想在一个.cpp上编码.
我不能在A班之前写B班.
你有什么想法解决我的问题吗?
最好的祝福.
mas*_*oud 28
将doSomething定义移到其类声明之外和之后B,也add可以A通过public-ing或friend-ing来访问它.
class B;
class A
{
void doSomething(B * b);
};
class B
{
public:
void add() {}
};
void A::doSomething(B * b)
{
b->add();
}
Run Code Online (Sandbox Code Playgroud)
在使用该类之前,必须具有类的定义B.编译器怎么会知道存在这样的函数B::add呢?
任一定义类B类之前A,或移动的主体A::doSomething到后级B已被定义,如
class B;
class A
{
B* b;
void doSomething();
};
class B
{
A* a;
void add() {}
};
void A::doSomething()
{
b->add();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76619 次 |
| 最近记录: |