简体   繁体   English

如何在if…else语句中正确放置Struct类型的Object?

[英]How do I properly put an Object of the Struct type in an if…else statement?

I'm trying to program a simple state machine for an assignment. 我正在尝试编写一个简单的状态机进行分配。 The assignment is to build a code that, given a string for input, can start at the “Cat” state, and perform actions until I run out of information. 任务是构建一个代码,给定输入字符串,该代码可以从“猫”状态开始,并执行操作,直到我用尽所有信息。

Here's a chart which portrays what I'm trying to do: 这是一张描述我要做什么的图表:

图表


Now, I've almost finished the code but there's a problem within a function. 现在,我几乎完成了代码,但是函数中存在问题。 I am getting the error "invalid operands to binary expression ('State' and 'State')" Can someone give me a hint on how to correct this problem and a brief explanation on what's wrong? 我收到错误“对二进制表达式无效的操作数(“状态”和“状态”)”,有人可以给我提示如何解决此问题,以及对错误之处的简要说明吗? The problem is with using struct types in an if..else statement. 问题在于在if..else语句中使用结构类型。

I have this portion of the code before int main(): 我在int main()之前有这部分代码:

struct State{
  string A, B;
};

State Cat = {"Meow", "Ignore"};
State Noise = {"Boing", "Thud"};
State Food = {"Lemons", "Cinnamon"};

State mystate = Cat;

 //my_state.A -> string 

And here is the function where the error is: 这是错误所在的函数:

void change_state(char c) {
  // on taking character c, it changes current state
  // If state is Cat and I get 1 , change to Food
  // If state is Cat and I get 2 , change to Noise
  // If state is Food and I get 1 , change to Noise
  // If state is Food and I get 2 , change to Cat
  // If state is Noise and I get 1 , change to Cat
  // If state is Noise and I get 2 , change to Food

  if (mystate == Cat){  //error 
    if (c == '1') {
      mystate = Food;
    }
    else {
      mystate = Noise;
    }
  }
  else if (mystate == Food) {
    if (c == '1') {
      mystate = Noise;
    }
    else {
      mystate = Cat;
    }
  }
  else  {
    if (c == '1') {
      mystate = Cat;
    }
    else {
      mystate = Food;
    }
  }
}

Any help will be appreciated! 任何帮助将不胜感激!

To compare custom types with == , you need to overload operator== for the type, to specify when two objects of that type are considered equal. 要将自定义类型与==进行比较,您需要为该类型重载operator== ,以指定何时将该类型的两个对象视为相等。

For example: 例如:

bool operator==(State const& left, State const& right) {
  return left.A == right.A && left.B == right.B;
}

Now when you use == on two State s, this function gets called. 现在,当您在两个State上使用==时,将调用此函数。

More info: Operator overloading 更多信息: 运算符重载

As @zenith pointed out, what you are asking for can be done, using operator overloading. 正如@zenith指出的那样,您可以通过使用运算符重载来完成您所要的操作。 However, using a struct as a state value doesn't really make sense. 但是,使用struct作为状态值实际上没有任何意义。 You can use an enum instead, which is more inline with your flow chart anyway: 您可以改用一个enum ,无论如何它更符合您的流程图:

enum State {Cat, Noise, Food};
string StateStrings[3][2];

...

StateStrings[Cat][0] = "Meow";
StateStrings[Cat][1] = "Ignore";

StateStrings[Noise][0] = "Boing";
StateStrings[Noise][1] = "Thud";

StateStrings[Food][0] = "Lemons";
StateStrings[Food][1] = "Cinnamon";

State mystate = Cat;

...

void change_state(char c)
{
  // on taking character c, it changes current state
  // If state is Cat and I get 1 , change to Food
  // If state is Cat and I get 2 , change to Noise
  // If state is Food and I get 1 , change to Noise
  // If state is Food and I get 2 , change to Cat
  // If state is Noise and I get 1 , change to Cat
  // If state is Noise and I get 2 , change to Food

  switch (mystate)
  {
    case Cat: {
      switch (c) {
        case '1': mystate = Food; break;
        case '2': mystate = Noise; break;
      }
      break;
    }
    case Noise: {
      switch (c) {
        case '1': mystate = Cat; break;
        case '2': mystate = Food; break;
      }
      break;
    }
    case Food: {
      switch (c) {
        case '1': mystate = Noise; break;
        case '2': mystate = Cat; break;
      }
      break;
    }
  }
}

First, I think you can define a constructor for the struct 首先,我认为您可以为struct定义一个构造函数

State(cosnt string& a, const string &b):A(a), B(b){}  

Second, besides the solution provided by zenith,you can define a member function operator== instead: 其次,除了zenith提供的解决方案之外,您还可以定义一个成员函数operator==
bool State::operator==(State const& right) { return this->A == right.A && this->B == right.B; }

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

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