繁体   English   中英

为什么我的运算子==不匹配

[英]Why does my operator== have no match

if(response=='y'){
    cout << "great. file saved, please send the file to me and you'll receive the package information and status.";
}
else if(response=='n'){
    cout << "exiting. please do it again correctly, thanks!";
}

上面的代码给了我以下编译错误:

错误:与“ operator ==”不匹配(操作数类型为“ std :: string {aka std :: basic_string}”和“ char”)|

我不知道问题是什么。

有人可以对此提供一些建议吗? 谢谢!

'y'是字符文字,并且没有将std::string与字符进行比较的转换运算符。 有一个运算符可与其他std::string对象进行比较,或与C风格的字符串进行比较,因此可以使用"y""n"代替。

'n'是一个charresponse是一个字符串。 字符串是字符数组。 我不知道如何为response分配值,但是如果要从istream中提取值,则必须先对其进行清理,然后才能使用它。 然后,您将要么必须使用

if(response=="y") //note the double quotes

比较整个字符串和长度为1的字符串

要么

if(response[0]=='y')

比较字符串中的一个字符。

暂无
暂无

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

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