C++ 错误 C2504:未定义基类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24725326/
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
error C2504: base class undefined
提问by Greg Treleaven
I have encountered this error many times before and eventually found solutions, but this one has me stumped. I have a class 'Mob' that is inherited by class 'Player'. This is Mob.h:
我之前多次遇到过这个错误并最终找到了解决方案,但是这个让我很难过。我有一个由“玩家”类继承的“暴徒”类。这是 Mob.h:
#pragma once
#include "PlayState.h"
#include "OmiGame/OmiGame.h"
#include "resources.h"
class PlayState;
class Mob
{
private:
int frames;
int width;
int height;
int time;
sf::Texture textureL;
sf::Texture textureR;
Animation animationL;
Animation animationR;
AnimatedSprite sprite;
bool moveLeft;
bool moveRight;
bool facingRight;
public:
void createMob(std::string l, std::string r, int frames, int width, int height, int time, int x, int y);
void updateMob(omi::Game *game, PlayState *state);
void drawMob(sf::RenderTarget &target);
void setLeft(bool b) { moveLeft = b; }
void setRight(bool b) { moveRight = b; }
bool isLeft() { return moveLeft; }
bool isRight() { return moveRight; }
sf::Vector2f getPosition() { return sprite.getPosition(); }
};
This is Player.h, as of now it is extremely simple:
这是 Player.h,到目前为止它非常简单:
#pragma once
#include "OmiGame/OmiGame.h"
#include "PlayState.h"
#include "Mob.h"
#include "resources.h"
class PlayState;
class Mob;
const int playerFrames = 8;
const int playerWidth = 16;
const int playerHeight = 48;
const int playerTime = 50;
const int playerX = 200;
const int playerY = 200;
class Player : public Mob
{ //the error occurs at this line//
public:
Player();
void update(omi::Game *game, PlayState *state);
void draw(sf::RenderTarget &target);
};
And, as you can probably guess, this is the error:
而且,正如您可能猜到的,这是错误:
error C2504: 'Mob' : base class undefined player.h
I have forward declared mob, I have hopefully fixed any circular dependencies. Please can someone help me?
我已经提前声明了 mob,我希望修复任何循环依赖。请有人可以帮助我吗?
回答by TheUndeadFish
Forward declaring doesn't help for class Player : public Mob
because the compiler needs the full definition for inheritance.
前向声明没有帮助,class Player : public Mob
因为编译器需要继承的完整定义。
So most likely one of your includes in Mob.h is bringing in Player.h which then puts Player ahead of Mob and thus triggers the error.
因此,您在 Mob.h 中包含的内容之一很可能是引入 Player.h,然后将 Player 置于 Mob 之前,从而触发错误。
回答by hemant c
I have got through the similar problem and I out solution and made it a thumb rule for me
我已经解决了类似的问题,我提出了解决方案,并为我制定了一个拇指规则
Solution / Thumb Rule
解决方案/经验法则
//File - Foo.h
#include "Child.h"
class Foo
{
//Do nothing
};
//File - Parent.h
#include "Child.h" // wrong
#include "Foo.h" // wrong because this indirectly
//contain "Child.h" (That is what is your condition)
class Parent
{
//Do nothing
Child ChildObj ; //one piece of advice try avoiding the object of child in parent
//and if you want to do then there are diff way to achieve it
};
//File - Child.h
#include "Parent.h"
class Child::public Parent
{
//Do nothing
};
Don't include the child in parent class .
不要将孩子包含在父类中。
if you want to know the way to have a child object in a parent class the refer the link Alternative
如果您想知道在父类中拥有子对象的方法,请参考链接Alternative
Thank you
谢谢
回答by Mark green
I know this is not the best way to deal with that problem, but it work for me at least. you can put all your other includes in the cpp file:
我知道这不是解决该问题的最佳方法,但至少对我有用。你可以把你所有的其他包含在 cpp 文件中:
#include "OmiGame/OmiGame.h"
#include "PlayState.h"
#include "Mob.h"
#include "resources.h"