【问题标题】:error C2504: base class undefined错误 C2504:基类未定义
【发布时间】:2014-09-03 16:59:30
【问题描述】:

我之前多次遇到这个错误并最终找到了解决方案,但是这个让我很难过。我有一个由“Player”类继承的“Mob”类。这是 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(); }
};

这是 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);
};

而且,您可能已经猜到了,这是错误:

error C2504: 'Mob' : base class undefined   player.h

我已经提前声明了 mob,我希望已经修复了任何循环依赖。请问有人可以帮我吗?

【问题讨论】:

  • 文件在同一个目录吗?
  • 你知道,如果你为所有私有成员提供访问器,它会以某种方式破坏封装......
  • @Deduplicator 它没有。只有四个私有成员具有访问器,因为暴民需要根据它们做出不同的反应,但他们需要共享这些成员,以便重用的代码也可以访问它们。
  • @Greg:所以他们是公共成员,即使代码试图欺骗你相信其他人。
  • @Deduplicator 没错,getter/setter 编码风格是有争议的,但我是通过这种方式学习 OO 编程的,所以它已经成为一种习惯。这完全取决于个人喜好和惯例。

标签: c++ class inheritance header


【解决方案1】:

前向声明对class Player : public Mob 没有帮助,因为编译器需要完整的继承定义。

所以很可能你在 Mob.h 中包含的一个是引入 Player.h,然后将 Player 置于 Mob 之前,从而触发错误。

【讨论】:

  • 删除 #include "PlayState.h" 有效。 PlayState 声明了 Player,但 Player 也声明了 PlayState,因此不需要该行。谢谢。
【解决方案2】:

我已经解决了类似的问题,我提出了解决方案并将其作为我的经验法则

解决方案/拇指规则

//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 
};

不要将孩子包含在父类中。

如果您想知道在父类中拥有子对象的方法,请参考链接Alternative

谢谢

【讨论】:

    【解决方案3】:

    我知道这不是解决该问题的最佳方法,但至少对我有用。您可以将所有其他包含在 cpp 文件中:

    #include "OmiGame/OmiGame.h"
    #include "PlayState.h"
    #include "Mob.h"
    #include "resources.h"
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2011-12-30
      • 2012-05-27
      相关资源
      最近更新 更多