C++ 如何正确地将结构类型的对象放入if…else语句中?

C++ 如何正确地将结构类型的对象放入if…else语句中?,c++,struct,C++,Struct,我正试图为一项任务编写一个简单的状态机。任务是构建一个代码,给定一个字符串作为输入,该代码可以从“Cat”状态开始,并执行操作,直到信息耗尽为止 下面是一个图表,描述了我正在尝试做的事情: 现在,我几乎完成了代码,但是函数中有一个问题。我得到了二进制表达式“State”和“State”的错误无效操作数。有人能给我一个如何纠正这个问题的提示,并简要解释一下什么是错误的吗?问题在于在if..else语句中使用结构类型 我在int main之前有这部分代码: struct State{ strin

我正试图为一项任务编写一个简单的状态机。任务是构建一个代码,给定一个字符串作为输入,该代码可以从“Cat”状态开始,并执行操作,直到信息耗尽为止

下面是一个图表,描述了我正在尝试做的事情:

现在,我几乎完成了代码,但是函数中有一个问题。我得到了二进制表达式“State”和“State”的错误无效操作数。有人能给我一个如何纠正这个问题的提示,并简要解释一下什么是错误的吗?问题在于在if..else语句中使用结构类型

我在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 
这是一个函数,其中的错误是:

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

任何帮助都将不胜感激

要将自定义类型与==进行比较,需要为该类型重载运算符==以指定该类型的两个对象何时被视为相等

例如:

bool operator==(State const& left, State const& right) {
  return left.A == right.A && left.B == right.B;
}
现在,当您在两个状态上使用==时,将调用此函数


更多信息:

要将自定义类型与==进行比较,需要为该类型重载运算符==以指定该类型的两个对象何时被视为相等

例如:

bool operator==(State const& left, State const& right) {
  return left.A == right.A && left.B == right.B;
}
现在,当您在两个状态上使用==时,将调用此函数


更多信息:

正如@zenith所指出的,可以使用操作符重载来完成您的请求。然而,使用结构作为状态值并没有真正意义。您可以改为使用枚举,它更符合您的流程图:

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

正如@zenith所指出的,您所要求的可以通过操作符重载来实现。然而,使用结构作为状态值并没有真正意义。您可以改为使用枚举,它更符合您的流程图:

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

首先,我认为可以为结构定义构造函数

State(cosnt string& a, const string &b):A(a), B(b){}  
第二,除了zenith提供的解决方案外,您还可以定义一个成员函数运算符==: bool State::operator==State const&right{ 返回this->A==right.A&&this->B==right.B;
}

首先,我认为可以为结构定义构造函数

State(cosnt string& a, const string &b):A(a), B(b){}  
第二,除了zenith提供的解决方案外,您还可以定义一个成员函数运算符==: bool State::operator==State const&right{ 返回this->A==right.A&&this->B==right.B;
}std::tielft.A,left.B==std::tielight.A,right.B可能更清晰。如果没有其他原因的话,当操作数因任何原因增长时,我会觉得简单得多。std::tieleft.A,left.B==std::tieleft.A,right.B可能更清晰。如果没有其他原因的话,当操作数出于任何原因增长时,我觉得简单多了。