我之前多次遇到这个错误并最终找到了解决方案,但是这个让我很难过。我有一个由“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
我已经转发了声明的暴民,我希望修复了任何循环依赖。请问有人可以帮我吗?
原文由 Greg Treleaven 发布,翻译遵循 CC BY-SA 4.0 许可协议
前向声明对
class Player : public Mob
没有帮助,因为编译器需要完整的继承定义。所以很可能你在 Mob.h 中包含的一个是引入 Player.h ,然后将 Player 置于 Mob 之前,从而触发错误。