如何使用枚举检查此条件 - C++
How to check this condition with enums - C++
Class方向
class Direction
{
public:
enum value
{
UP,
RIGHT,
DOWN,
LEFT,
STAY
};
};
Class点
#include "Direction.h"
class Point {
int x , y;
public:
Point() { x=0; y=0; };
Point(int x1 , int y1) : x(x1) , y(y1) {};
void setX(int newX) { x = newX; };
void setY(int newY) { y = newY; };
int getX() { return x; }
int getY() { return y; }
void move(Direction d) {
if(d==UP);
};
问题是 if(d==UP)
我不明白如何检查条件 我收到错误检查。
有什么想法吗?
任何帮助将不胜感激。
UP
是在 class Direction
内声明的,所以在那个范围之外你应该写 Direction::UP
来引用它:
if (...something... == Direction::UP)
...
您的 Direction
class 创建了 value
枚举类型,但还没有任何数据成员,因此不清楚您可能想要 d
中的内容与 Direction::UP
进行比较。您可以通过在 class Direction
中的最终 };
之前插入额外的一行来添加数据成员:
value value_;
那么你的比较将变成:
if (d.value_ == Direction::UP)
...
综上所述,如果您的 class Direction
除了 enum
之外不包含任何其他内容,您可以考虑完全删除 class 部分并简单地声明枚举一个人:
enum Direction { UP, ... };
那么您的比较将简化为:
if (d == UP)
...
if(d.value == UP)
d 属于类型 class Direction
而不是类型 Enum value
。
因此你不能比较 d
和 `UP
我认为您不需要创建 class Direction
。简单的枚举就足够了。
enum class Direction
{
UP,
DOWN,
LEFT,
RIGHT,
STAY,
};
然后你可以编写类似下面的函数:
void do_move(Direction d)
{
switch (d) {
case Direction::LEFT:
cout << "LEFT" << endl;
break;
case Direction::RIGHT:
cout << "RIGHT" << endl;
break;
case Direction::UP:
cout << "UP" << endl;
break;
case Direction::DOWN:
cout << "DOWN" << endl;
break;
case Direction::STAY:
cout << "STAY" << endl;
break;
default:
break;
}
}
简单地称之为:
Direction d = Direction::UP;
do_move(d);
do_move(Direction::STAY);
你真正想要的是 Direction
成为枚举,而不是 class:
enum Direction { UP, RIGHT, DOWN, LEFT, STAY };
现在您可以像这样调用 move
:
move(UP);
您的 if(d==UP);
将按预期工作。
我注意到你的 Direction
class 除了枚举之外没有任何东西,在这种情况下你可以简单地:
enum class Direction {
UP,
...
};
区别在于enum class
枚举将被封装在其中。您仍然需要做 if (d == Direction::UP)
.
如果将其设为常规枚举:
enum Direction {
UP,
...
};
然后你可以直接if (d == UP)
像你原来的代码一样
此外,这样你可以得到Direction d
,比value d
或Direction::value d
更显眼一点。
您现在拥有的是一个常规枚举,它没有封装在 value
中,而是封装在 value
所在的 Direction
class 中。只能在Direction
内直接寻址,但在Direction
外,必须指定封装范围class.
Class方向
class Direction
{
public:
enum value
{
UP,
RIGHT,
DOWN,
LEFT,
STAY
};
};
Class点
#include "Direction.h"
class Point {
int x , y;
public:
Point() { x=0; y=0; };
Point(int x1 , int y1) : x(x1) , y(y1) {};
void setX(int newX) { x = newX; };
void setY(int newY) { y = newY; };
int getX() { return x; }
int getY() { return y; }
void move(Direction d) {
if(d==UP);
};
问题是 if(d==UP)
我不明白如何检查条件 我收到错误检查。
有什么想法吗? 任何帮助将不胜感激。
UP
是在 class Direction
内声明的,所以在那个范围之外你应该写 Direction::UP
来引用它:
if (...something... == Direction::UP)
...
您的 Direction
class 创建了 value
枚举类型,但还没有任何数据成员,因此不清楚您可能想要 d
中的内容与 Direction::UP
进行比较。您可以通过在 class Direction
中的最终 };
之前插入额外的一行来添加数据成员:
value value_;
那么你的比较将变成:
if (d.value_ == Direction::UP)
...
综上所述,如果您的 class Direction
除了 enum
之外不包含任何其他内容,您可以考虑完全删除 class 部分并简单地声明枚举一个人:
enum Direction { UP, ... };
那么您的比较将简化为:
if (d == UP)
...
if(d.value == UP)
d 属于类型 class Direction
而不是类型 Enum value
。
因此你不能比较 d
和 `UP
我认为您不需要创建 class Direction
。简单的枚举就足够了。
enum class Direction
{
UP,
DOWN,
LEFT,
RIGHT,
STAY,
};
然后你可以编写类似下面的函数:
void do_move(Direction d)
{
switch (d) {
case Direction::LEFT:
cout << "LEFT" << endl;
break;
case Direction::RIGHT:
cout << "RIGHT" << endl;
break;
case Direction::UP:
cout << "UP" << endl;
break;
case Direction::DOWN:
cout << "DOWN" << endl;
break;
case Direction::STAY:
cout << "STAY" << endl;
break;
default:
break;
}
}
简单地称之为:
Direction d = Direction::UP;
do_move(d);
do_move(Direction::STAY);
你真正想要的是 Direction
成为枚举,而不是 class:
enum Direction { UP, RIGHT, DOWN, LEFT, STAY };
现在您可以像这样调用 move
:
move(UP);
您的 if(d==UP);
将按预期工作。
我注意到你的 Direction
class 除了枚举之外没有任何东西,在这种情况下你可以简单地:
enum class Direction {
UP,
...
};
区别在于enum class
枚举将被封装在其中。您仍然需要做 if (d == Direction::UP)
.
如果将其设为常规枚举:
enum Direction {
UP,
...
};
然后你可以直接if (d == UP)
像你原来的代码一样
此外,这样你可以得到Direction d
,比value d
或Direction::value d
更显眼一点。
您现在拥有的是一个常规枚举,它没有封装在 value
中,而是封装在 value
所在的 Direction
class 中。只能在Direction
内直接寻址,但在Direction
外,必须指定封装范围class.