簡體   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