12

Base class has incomplete type

What exactly does this error mean and how can I fix it? I have tried forward declaring the class by doing class Entity in my EntityPhysics header but it did not work.

Here is my 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__) */

And my 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__) */

I think it may have to do with me including Entity.h in the child class, but if I do that I get the same error.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Nik
  • 1,033
  • 2
  • 11
  • 28
  • 2
    You cannot forward declare a class and then use that forward declaration for, say, inheritance. All you can do with a forward declared type is to declare a pointer or reference to said type. Also not that both of these classes contain pure virtual functions and, as such, you cannot create an instance of either directly. – Ed S. Oct 01 '12 at 20:12
  • 1
    Also, you should not be using double underscores in your identifiers. Identifiers beginning with a double underscore are reserved for use by the compiler. – Ed S. Oct 01 '12 at 20:13
  • @EdS., Identifiers *containing* double underscores are reserved ([reference](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)). It took me many times reading it over before I got that message for some reason. In addition, starting a global scope identifier with an underscore puts it in the same boat. – chris Oct 01 '12 at 20:16
  • Xcode auto generates those double underscores, so I thought that it would be ok since the tool that comes with the compiler does it... – Nik Oct 01 '12 at 20:19
  • @notrodash - the tool is wrong. Names that contain two underscores and names that begin with an underscore followed by a capital letter are reserved. – Pete Becker Oct 01 '12 at 20:25
  • @PeteBecker maybe its different for the apple compiler, I dont know. I have been doing it this way for ages and I never had any problems – Nik Oct 01 '12 at 20:27
  • @notrodash - it's not different; the names are reserved. Lots of people think it's okay, and do it that way. They just haven't run into a problem with it. Names like that are used throughout the standard library; take a look at the standard library headers to see this. The reason for this is so that they won't collide with your names. – Pete Becker Oct 01 '12 at 20:31

3 Answers3

12

This is most likely due to a circular include, and the way to fix this is to remove includes where you don't need them.

In Entity.h you don't need:

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

You can, and should, instead forward-declare the types. Same for EntityPhysics.h and:

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

#include "OgreInit.h"

the only one you actually need is Entity.h.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I need some of the other classes in the cpp file that goes with it. – Nik Oct 01 '12 at 20:20
  • Looks like `OrgeInit.h` is still required in `Entity.h`, because `Ogre::Vector3::ZERO` is defined there. – Rost Oct 01 '12 at 20:21
  • @notrodash the cpp files can include the other files, that's fine. – Luchian Grigore Oct 01 '12 at 20:21
  • OgreInit is actually a custom header that I created that loads ogre for me. – Nik Oct 01 '12 at 20:22
  • @LuchianGrigore I have tried the edits and the error still exists. I was always told that I can forward declare to fix circular includes... – Nik Oct 01 '12 at 20:25
  • This also could be just simple syntax error if e.g. some closing bracket `}` is missing in some header or `;` is missing after class definition. Check brackets to be sure it's not a problem. – Rost Oct 01 '12 at 20:26
  • @Rost I just checked them and they all seem ok. Also Xcode would point this out. – Nik Oct 01 '12 at 20:26
  • @LuchianGrigore After checking every single file that could possibly relate to my problem I found that there was a circular include. Thank you very much :) – Nik Oct 01 '12 at 20:54
0

I had a similar error message which i resolved by moving the #includes lower down the header file so that the class definition and method definition that the other #includes desire come earlier than the other header includes.

Rohit
  • 13
  • 3
0

It occurs when we want to use as based class but it has not been completed(some thing wrong in based class). just check the based class whether everything is fine or not

  • 3
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 09 '21 at 19:10