【发布时间】:2021-09-04 00:55:24
【问题描述】:
我已经创建了这个函数,它是一个类的成员函数,这个函数中使用的 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;
}
}
【问题讨论】:
-
显示您的完整代码。
-
如果车轮状态为“正在移动”,则代码开始移动购物车,这似乎很奇怪。
-
我的猜测:
getwheelstate不会返回正确的std::string而是char const *或char *,也就是 C 风格的字符串。然后你将比较指针,而不是字符串内容。我们需要一个minimal reproducible example 来确定。 -
请告诉我们
getwheelstate()是如何实现的,因为该功能与问题相关。我们不关心cout的8行。 -
@churill class wheel { 私有:字符串状态;公共:无效setwheelstate(字符串s){状态=s; } string getwheelstate() { 返回状态; } };