本文介绍了错误C2504:基类未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我曾多次遇到过这个错误,并最终找到了解决方案,但是这一个让我难住。我有一个类'Mob',由'Player'类继承。这是Mob.h:

  #pragma once 
#includePlayState.h
#include OmiGame / OmiGame.h
#includeresources.h

类PlayState;

class Mob
{
private:
int frames;
int width;
int height;
int time;

sf :: Texture textureL;
sf :: Texture textureR;
动画animationL;
动画animationR;
AnimatedSprite sprite;
bool moveLeft;
bool moveRight;
bool facingRight;
$ b $ 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 
#includeOmiGame / OmiGame.h
#includePlayState.h
#include Mob.h
#includeresources.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;
$ b class类播放器:public Mob
{//错误发生在这一行//
public:
Player();
void update(omi :: Game *游戏,PlayState *状态);
void draw(sf :: RenderTarget& target);
};

而且,正如您大概猜测的那样,这是错误:

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

我向前宣布暴徒,我希望能够修复任何循环依赖。请问有人可以帮我吗?

/ code>,因为编译器需要完整的继承定义。



所以你最有可能在Mob.h中包含一个包含在Player.h中的Player.h,将玩家置于Mob之前,从而触发该错误。


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:

#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:

#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?

解决方案

Forward declaring doesn't help for class Player : public Mob because the compiler needs the full definition for inheritance.

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.

这篇关于错误C2504:基类未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:12