我下面的代码生成错误
‘WorldObject’:[未定义的基类(从德语翻译)]
为什么是这样?这是产生此错误的代码:
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
下面是 WorldObject 类的代码文件:
游戏对象.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
该文件中还有其他一些类,但我认为它们无关紧要。也许有一个我没有看到的小错误,但我不明白为什么会产生这个错误。当您需要更多代码时,请随意。
原文由 JimRaid 发布,翻译遵循 CC BY-SA 4.0 许可协议
If you have any source file that includes
GameObjects.h
beforeProjectilObject.h
or does not includeProjectilObject.h
directly, then the compiler will first find the declaration ofProjectilObject
在知道WorldObject
是什么之前,通过GameObjects.h
中的包含。那是因为GameObjects.h
首先包含ProjectilObject.h
然后声明WorldObject
。在这种情况下,包含在GameObjects.h
中的ProjectilObject.h
将不起作用,因为_GAMEONJECTCODE_H_
已经定义。为避免这种情况,请确保在源文件中包含
ProjectilObject.h
而不是GameObjects.h
,或者使用 前向声明。