类的前向声明/不完整类型的无效使用

时间:2014-06-24 23:14:42

标签: c++ inheritance compiler-errors

我有六个班级,显然这不是我的实际代码,只是一个简单的版本

BaseA:

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;
};

BaseB:

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;
};

BASEC

class BaseC {
public:
    BaseC();
    virtual ~BaseC();
    void update() { // the body functions would normally be in a seperate file
        // do whatever
    }
};

然后我有三个其他类,A,B和C继承它们各自的基类。我在list_of_bs / list_of_cs中添加了B或C的实例,因此在调用更新函数时可以运行自己的代码。

问题是,它在各种文件中出现错误“类的前向声明/不完整类型的无效使用”。如何设置我的系统以使其没有这些错误?

如果您想查看我的实际代码,可以在此处找到:https://github.com/Ja-ake/openworld

阅读自述文件。

1 个答案:

答案 0 :(得分:2)

您获得的错误是使用前向声明然后尝试调用方法或访问成员而不提供完整声明的结果。

修复是为了确保在调用任何方法之前始终#include包含完整类定义的.h文件(不一定是包含方法定义的cc文件)类。

相关问题