【发布时间】: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