简体   繁体   English

如何用枚举检查这种情况-C ++

[英]How to check this condition with enums - C++

Class Direction 班级方向

class Direction
{
public:
 enum value
 {
  UP,
  RIGHT,
  DOWN,
  LEFT,
  STAY
 };
};   

Class Point 类点

 #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);
        };

The problem is if(d==UP) I don't understand how can I check the condition I receive an error checking this. 问题是if(d==UP)我不知道如何检查我收到的错误。

Any ideas? 有任何想法吗? Any help will be appreciated. 任何帮助将不胜感激。

UP is declared inside class Direction , so outside that scope you should write Direction::UP to refer to it: UPclass Direction中声明,因此在该范围之外,您应该编写Direction::UP来引用它:

if (...something... == Direction::UP)
    ...

Your Direction class creates the value enumeration type, but doesn't have any data members yet, so it's not clear what in d you might want to compare to Direction::UP . 你的Direction类创建value枚举类型,但没有任何数据成员还没有,所以目前还不清楚在什么d你可能要比较Direction::UP You could add a data member by inserting an extra line just before the final }; 您可以通过在最后一个};之前插入额外的一行来添加数据成员}; in class Direction : class Direction

value value_;

Then your comparison would become: 那么您的比较将变为:

if (d.value_ == Direction::UP)
    ...

All that said, if your class Direction is not going to contain anything other than the enum , you might consider getting rid of the class part altogether and simply declaring the enum alone: 话虽如此,如果您的class Direction不包含enum以外的任何内容,则可以考虑完全放弃该类部分,而只声明一个枚举:

enum Direction { UP, ... };

Then your comparison would simplify to: 然后,您的比较将简化为:

if (d == UP)
    ...

I think you didn't need to having create class Direction . 我认为您不需要创建类Direction Simple enum is enough. 简单的枚举就足够了。

enum class Direction
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    STAY,
};

Then you can write similar function to following one: 然后,您可以编写类似于以下功能的函数:

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;
    }
}

And simply call it: 并简单地称之为:

Direction d = Direction::UP;
do_move(d);
do_move(Direction::STAY);

What you really want is Direction to be an enum, not a class: 你真正需要的是Direction是一个枚举,而不是一类:

enum Direction { UP, RIGHT, DOWN, LEFT, STAY };

Now you can call move simply like this: 现在,您可以像这样简单地调用move

move(UP);

And your if(d==UP); 而你的if(d==UP); will work as expected. 将按预期工作。

I notice your Direction class doesn't have anything except an enum in it, in that case you could simply: 我注意到您的Direction类除了枚举外什么都没有,在这种情况下,您可以简单地:

enum class Direction {
  UP,
  ...
};

The difference is with enum class the enumerations will be encapsulated in it. enum class不同的是, enum class将封装在其中。 You will still have to do if (d == Direction::UP) . if (d == Direction::UP)您仍然必须做。

If you make it a regular enum: 如果将其设为常规枚举:

enum Direction {
  UP,
  ...
};

Then you can directly if (d == UP) as in your original code. 然后,您可以直接按照原始代码中的if (d == UP)进行操作。

Also, this way you can have Direction d which is a little more conspicuous than value d or Direction::value d . 同样,通过这种方式,您可以使Direction dvalue dDirection::value d更加明显。

What you have now is a regular enum which is not encapsulated in value but is encapsulated in the Direction class in which value is. 你现在有什么是正枚举未封装的value ,但被封装在Direction类,其中value是。 You can only address it directly inside Direction , but outside of it, you will have to specify the scope of the encapsulating class. 您只能在Direction内部直接解决它,但在它外部,您必须指定封装类的范围。

if(d.value == UP)

d is of Type class Direction not of Type Enum value . d是Type class Direction而不是Enum value类型。 Hence you cannot compare d and `UP 因此,您无法比较d和`UP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM