所以我在做一个游戏,我有一个有哪个方向,玩家目前所面临的,向上的枚举,留下了一个Player类等不同类中的枚举不兼容?
Player.h
#include "Fireball.h"
class Player
{
// Some stuff
Fireball fireballs;
void update();
private:
enum direction {up, left, down, right, upLeft, upRight, downLeft, downRight} playerDir;
};
我也有一个魔法类,我会从Fireball中获取特定的法术。 Spell类的枚举与Player类中的枚举相同,因为我希望能够在更新咒语实例时将玩家的当前方向作为参数传递,并将该咒语移动到方向。
Spell.h
class Spell
{
// Some stuff
protected:
enum direction {up, left, down, right, upLeft, upRight, downLeft, downRight};
};
。
Fireball.h
#include "Spell.h"
class Fireball : public Spell
{
public:
void updateFireballs(direction fireballDir);
};
。
Player.cpp
#include "Player.h"
void Player::update()
{
fireballs.updateFireballs(playerDir);
}
当我尝试,并通过playerDir作为参数传递给updateFireballs功能,它抱怨说,它不能“球员::方向”转化为“拼写::方向”。
如何将不同类中的枚举作为另一个类中的函数的参数传递?
为什么要在两个类中定义单独的枚举,尤其是如果要交替使用它们?在两个类别之外有一个共同的定义会更有意义。 –
即使你在类Player外面定义'direction',在里面你仍然可以说'using direction = :: direction;'使'Player :: direction'成为全局'direction'的同义词。但也许你有类似“地图” - 方向通常属于地图。 – MSalters