11

I know it is one of the constant ask question so i get this error

'WorldObject': [Base class undefined (translated from german)]

Here is the code which produce this error:

ProjectilObject.h:

#pragma once

#ifndef _PROJECTILOBJECT_H_
#define _PROJECTILOBJECT_H_

#include "GameObjects.h"

class ProjectilObject: public WorldObject
{
public:
    ProjectilObject(IGameObject* parent,int projectiltype);

    void deleteyourself();
protected:
virtual void VProcEvent( long hashvalue,    std::stringstream &stream);
    virtual void VInit();
    virtual void VInitfromStream( std::stringstream &stream     );
    virtual void VonUpdate();
    virtual void VonRender();
private:
    vec3 vel;

    float lifetime;
    float lifetimeend;

    vec3 target;

    int m_projectiltype;
};

#endif

Here is the code file from the WorldObject class:

GameObjects.h:

#pragma once

#ifndef _GAMEONJECTCODE_H_
#define _GAMEONJECTCODE_H_

#include "IGameObject.h"
#include "Sprite.h"
#include "GamePath.h"
#include "HashedString/String.h"
#include "IAttribute.h"
#include "CharacterObjects.h"
#include "ProjectilObject.h"

[...]

class WorldObject: public IGameObject, public MRenderAble
{
public:
    WorldObject(IGameObject* parent);
    virtual bool IsDestroyAble();
    virtual bool IsMageAble();
    virtual bool IsRenderAble();
protected:
    virtual void VProcEvent( long hashvalue, std::stringstream &stream);
    virtual void VonUpdate();
    virtual void VonRender();
    virtual void VInit() =0;
    virtual void VInitfromStream( std::stringstream &stream ) =0;
    virtual void VSerialize( std::stringstream &stream );

    vec3 poscam;    
};

[...]

#endif

There are some other Classes in this file but they shouldn't interrupt, I think. Maybe there is a tiny error I didn't saw but I don't understand why this error is produced. When you need more of the code feel free to ask because i think it would only disturb.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
JimRaid
  • 125
  • 1
  • 1
  • 9
  • Did you mean `MRenderable` instead of `MRenderAble`? Also, you've got circular dependencies going on there between the two files. You need a forward declaration instead. – chris May 20 '12 at 17:32
  • @chris Same error with and without a forward declaration. `MRenderable` and `MRenderAble` has nothing to do with the problem. I think. `MRenderAble` is another class I created in the file "IAttribute.h". – JimRaid May 20 '12 at 17:38
  • Well, you need to include a full class in order to inherit it, so a forward declaration is useless there, but in your `GameObjects.h`, a forward declaration of `ProjectilObject.h` would probably suffice. – chris May 20 '12 at 17:45
  • 1
    You can only inherit from a *complete* type. – Kerrek SB May 20 '12 at 17:50
  • @JimRaid, Did you edit your code to fix mistakes??? gztomas had a correct answer according to your original post but you removed the broken code on May 20 at 17:37, while gztomas was working on his answer. Don't do that! We must be able to see the error in order to understand the answer. – pixelgrease Nov 14 '22 at 21:37

4 Answers4

12

If you have any source file that includes GameObjects.h before ProjectilObject.h or does not include ProjectilObject.h directly, then the compiler will first find the declaration of ProjectilObject through the include in GameObjects.h before knowing what WorldObject is. That is because GameObjects.h first includes ProjectilObject.h and then declares WorldObject. In that case the include of GameObjects.h present in ProjectilObject.h won't work because _GAMEONJECTCODE_H_ will be already defined.

To avoid this, either be sure to include ProjectilObject.h instead of GameObjects.h in your source file, or use forward declarations.

Community
  • 1
  • 1
gztomas
  • 3,030
  • 3
  • 27
  • 38
  • 1
    The link on forward declarations specifically states that you cannot use it to define a base class – IAmTheAg Nov 23 '14 at 03:15
5

It's hard to answer this question without looking at the whole code. Even a misplaced brace could count. Check your namespaces - are you sure the WorldObject is in the same namespace?

I suggest you use the #pragma message by placing it near the WorldObject definition and checking the compiler output:

#pragma message ("World object is defined")

If it does not show up, move the pragma to the parent .h file and check the compiler output again. With this you can easily locate the error.

Vladimir Sinenko
  • 4,629
  • 1
  • 27
  • 39
  • Namespace - This was the underlying problem to my Base class undefined error. Once I determined that I did not have circular dependency, I saw that the base class in question was not qualified by the namespace. – Big Al Oct 22 '21 at 21:15
2
class WorldObject;
class ProjectilObject: public WorldObject

You are forward declaring WorldObject, but to inherit from a class you need the definition, so you have to include the header of WorldObject.

0

In my case: i delete derived class include from base class header file.

for example:

file 1 : #include "B.h"

-> A()

file 2:

-> B() : A()

solution : delete #include "B.h" from file1

rkilic
  • 1