c++ - 基类有不完整的类型错误

标签 c++ oop

Base class has incomplete type

这个错误到底是什么意思,我该如何解决?我曾尝试通过在我的 EntityPhysics header 中执行 class Entity 来向前声明该类,但它没有用。

这是我的 Entity.h

#ifndef __Game__Entity__
#define __Game__Entity__

#include <iostream>
#include <string>

#include "OGRE/Ogre.h"

#include "OgreInit.h"

class Entity{
public:
    Entity(std::string entityId, std::string mesh, Ogre::Vector3 position = Ogre::Vector3::ZERO, Ogre::Vector3 rotation = Ogre::Vector3::ZERO);
    virtual ~Entity() = 0;

    void setPosition(Ogre::Vector3 position);
    Ogre::Vector3 getPosition();
    void setRotation(Ogre::Vector3 rotationIncrease);
    Ogre::Vector3 getRotation();
    void setMesh(std::string meshName);
    std::string getMesh();
    virtual void tick() = 0;
    void removeEntity();

    Ogre::Entity getEntity();
    Ogre::SceneNode getSceneNode();

    std::string entityId;
protected:
    Ogre::Entity *ent;
    Ogre::SceneNode *nod;
};

#endif /* defined(__Game__Entity__) */

还有我的 EntityPhysics.h

#ifndef __Game__EntityPhysics__
#define __Game__EntityPhysics__

#include <iostream>
#include <string>
#include "OGRE/Ogre.h"
#include "OgreBulletCollisionsBoxShape.h"
#include "OgreBulletDynamicsRigidBody.h"

#include "Entity.h"
#include "OgreInit.h"

class EntityPhysics: public Entity //error occurs here: "Base class has incomplete type"
{
public:
    EntityPhysics(std::string pentityId, std::string mesh, Ogre::Vector3 position, Ogre::Vector3 rotation, /*Physics Specific "stuff"*/std::string shapeForm = "BoxShape", float friction = 1.0, float restitution = 0.0, float mass = 1.0);
    virtual ~EntityPhysics() = 0;
    virtual void tick() = 0;
private:
    float friction, restitution, mass;

    OgreBulletCollisions::CollisionShape *collisionShape;
    OgreBulletDynamics::RigidBody *rigidBody;
};

#endif /* defined(__Game__EntityPhysics__) */

我认为这可能与我在子类中包含 Entity.h 有关,但如果我这样做,我会得到同样的错误。

最佳答案

这很可能是由于循环包含,解决此问题的方法是删除不需要的包含。

Entity.h 中你不需要:

#include "OGRE/Ogre.h"
#include "OgreInit.h"

您可以而且应该改为前向声明类型。 EntityPhysics.h 和:

相同
#include "OGRE/Ogre.h"
#include "OgreBulletCollisionsBoxShape.h"
#include "OgreBulletDynamicsRigidBody.h"

#include "OgreInit.h"

您真正需要的唯一一个是 Entity.h

关于c++ - 基类有不完整的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12680287/

相关文章:

c++ - 从 vector 中删除空元素

javascript - JavaScript 中动态类型运算符的双重调度

java - 什么时候初始化成员?

c++ - 为类的成员创建别名

Windows 上 Linux 的 C++ 开发

c++ - 将 C++ 模板参数限制为子类

Python - 属性错误

java - 对象数据库查询执行

c# - 派生类对象如何添加到基类对象列表

c++ - BMP Exporter 不工作 C++