无法将字符串返回函数与字符串进行比较

时间:2021-06-19 18:01:30

标签: c++ function class

我制作了这个函数,它是一个类的成员函数,这个函数中使用的 get 函数是另一个返回字符串的类的函数,但是当我将它与字符串进行比较时,它不起作用,即 if - 语句未执行

void printcarwheelstate() {

  if(w.getwheelstate()=="Moving")
  {
      setcartomoving();

  cout<<"Wheel 1 is "<<arr[0]<<endl;
    cout<<"Wheel 2 is "<<arr[1]<<endl;
     cout<<"Wheel 3 is "<<arr[2]<<endl;
      cout<<"Wheel 4 is "<<arr[3]<<endl;
  }
  else
  {
      setcartostopped();
      cout<<"Wheel 1 is "<<arr[0]<<endl;
    cout<<"Wheel 2 is "<<arr[1]<<endl;
     cout<<"Wheel 3 is "<<arr[2]<<endl;
      cout<<"Wheel 4 is "<<arr[3]<<endl;
  }
}

1 个答案:

答案 0 :(得分:-1)

尝试打印 w.getwheelstate() 以查看它返回的内容。 你检查过大写和小写字母吗?因为它们在比较字符串时存在差异,并且它们的ascii代码不同。

你也可以试试这个:

tmp = w.getwheelstate().compare("Moving")
if (tmp == 0)
{
//body
}