Base class undefined

2020-06-16 02:06发布

问题:

My code below generates the error

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

Why is this? Here is the code which produces this error:

ProjectilObject.h:

#pragma once

#ifndef _PROJECTILOBJECT_H_
#define _PROJECTILOBJECT_H_

#include "GameObjects.h"
class WorldObject;
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"

...

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 matter, I don't 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.

回答1:

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.



回答2:

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.