簡體   English   中英

如何在if…else語句中正確放置Struct類型的Object?

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

我正在嘗試編寫一個簡單的狀態機進行分配。 任務是構建一個代碼,給定輸入字符串,該代碼可以從“貓”狀態開始,並執行操作,直到我用盡所有信息。

這是一張描述我要做什么的圖表:

圖表


現在,我幾乎完成了代碼,但是函數中存在問題。 我收到錯誤“對二進制表達式無效的操作數(“狀態”和“狀態”)”,有人可以給我提示如何解決此問題,以及對錯誤之處的簡要說明嗎? 問題在於在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;
    }
  }
}

任何幫助將不勝感激!

要將自定義類型與==進行比較,您需要為該類型重載operator== ,以指定何時將該類型的兩個對象視為相等。

例如:

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

現在,當您在兩個State上使用==時,將調用此函數。

更多信息: 運算符重載

正如@zenith指出的那樣,您可以通過使用運算符重載來完成您所要的操作。 但是,使用struct作為狀態值實際上沒有任何意義。 您可以改用一個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;
    }
  }
}

首先,我認為您可以為struct定義一個構造函數

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

其次,除了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