Base class undefined

My code below generates an error

'WorldObject': [Base class undefined (translation from German)]

Why is this? Here is the code that causes 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 several other classes in this file, but they shouldn't matter, I don't think so. Perhaps there is a small error that I have not seen, but I do not understand why this error occurs. When you need more code, feel free to.

+7
source share
2 answers

If you have a source file that includes GameObjects.h prior to ProjectilObject.h or does not include ProjectilObject.h directly, then the compiler will first find the ProjectilObject declaration through include in GameObjects.h before knowing what WorldObject is. This is because GameObjects.h first includes ProjectilObject.h and then declares WorldObject . In this case, including GameObjects.h in ProjectilObject.h will not work, because _GAMEONJECTCODE_H_ will already be defined.

To avoid this, be sure to include ProjectilObject.h instead of GameObjects.h in the source file or use advanced ads .

+9
source

It is hard to answer this question without looking at the whole code. Even an erroneous brace could count. Check namespaces - Are you sure the WorldObject is in the same namespace?

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

#pragma message ("World object defined")

If it does not appear, move the pragma to the parent .h file and check the compiler output again. However, you can easily find the error.

+2
source

All Articles