C++ 基类未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10675618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base class undefined
提问by JimRaid
My code below generates the error
我下面的代码生成错误
'WorldObject': [Base class undefined (translated from german)]
'WorldObject':[未定义基类(从德语翻译)]
Why is this? Here is the code which produces this error:
为什么是这样?这是产生此错误的代码:
ProjectilObject.h:
投影对象.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:
以下是 WorldObject 类的代码文件:
GameObjects.h:
游戏对象.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.
该文件中还有一些其他类,但我认为它们无关紧要。也许有一个我没有看到的小错误,但我不明白为什么会产生这个错误。当您需要更多代码时,请随意。
回答by gztomas
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.
如果你有任何源文件包含GameObjects.h
beforeProjectilObject.h
或者不ProjectilObject.h
直接包含,那么编译器会先ProjectilObject
通过include in找到的声明GameObjects.h
才知道是什么WorldObject
。那是因为GameObjects.h
首先包含ProjectilObject.h
然后声明WorldObject
. 在这种情况下,包含GameObjects.h
present inProjectilObject.h
将不起作用,因为_GAMEONJECTCODE_H_
已经定义了。
To avoid this, either be sure to include ProjectilObject.h
instead of GameObjects.h
in your source file, or use forward declarations.
为避免这种情况,请确保在源文件中包含ProjectilObject.h
而不是包含GameObjects.h
,或使用前向声明。
回答by Vladimir Sinenko
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?
不看整个代码很难回答这个问题。即使是放错了位置的支架也能算数。检查您的命名空间 - 您确定 WorldObject 在同一个命名空间中吗?
I suggest you use the #pragma message by placing it near the WorldObject definition and checking the compiler output:
我建议您通过将 #pragma 消息放在 WorldObject 定义附近并检查编译器输出来使用它:
#pragma message ("World object is defined")
#pragma message(“世界对象已定义”)
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.
如果未显示,请将编译指示移至父 .h 文件并再次检查编译器输出。有了这个,您可以轻松定位错误。