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 if(d==up) don't understand how can check condition receive error checking this.

any ideas? appreciated.

up declared inside class direction, outside scope should write direction::up refer it:

if (...something... == direction::up)     ... 

your direction class creates value enumeration type, doesn't have data members yet, it's not clear in d might want compare direction::up. add data member inserting line before final }; in class direction:

value value_; 

then comparison become:

if (d.value_ == direction::up)     ... 

all said, if class direction not going contain other enum, might consider getting rid of class part altogether , declaring enum alone:

enum direction { up, ... }; 

then comparison simplify to:

if (d == up)     ... 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -