1 c++ inheritance compiler-errors
我有六个类,显然这不是我的实际代码,只是一个简单的版本
基地A:
class BaseA {
public:
BaseA();
virtual ~BaseA();
void update() { // the body functions would normally be in a seperate file
for (auto iter : list_of_bs) {
iter->update();
}
}
private:
vector<BaseB*>* list_of_bs;
};
Run Code Online (Sandbox Code Playgroud)
基地B:
class BaseB {
public:
BaseB();
virtual ~BaseB();
void update() { // the body functions would normally be in a seperate file
for (auto iter : list_of_cs) {
iter->update();
}
}
private:
vector<BaseC*>* list_of_cs;
};
Run Code Online (Sandbox Code Playgroud)
基C
class BaseC {
public:
BaseC();
virtual ~BaseC();
void update() { // the body functions would normally be in a seperate file
// do whatever
}
};
Run Code Online (Sandbox Code Playgroud)
然后我还有另外三个类,A、B 和 C,它们继承了它们各自的基类。我将 B 或 C 的实例添加到 list_of_bs/list_of_cs 中,以便在调用更新函数时可以运行自己的代码。
问题是,它在各种文件中出现错误“类的前向声明/不完整类型的无效使用”。如何设置我的系统,使其不出现这些错误?
如果你想看我的实际代码,你可以在这里找到它:https : //github.com/Ja-ake/openworld
阅读自述文件。
您得到的错误是使用前向声明然后尝试调用方法或访问成员而不提供完整声明的结果。
解决方法是确保您始终#include将.h包含完整的类定义(不一定是文件cc调用类的任何方法之前包含方法定义文件)。
| 归档时间: |
|
| 查看次数: |
4655 次 |
| 最近记录: |