简体   繁体   中英

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!";
}

The above code gives me the following compile error:

error: no match for 'operator==' (operand types are 'std::string {aka std::basic_string}' and 'char')|

I don't know what the issue is.

Could anyone give some advice on this? Thanks!

'y' is a character literal, and there is no conversion operator comparing std::string with a character. There is an operator for comparing to other std::string objects, or to C-style strings, so using "y" and "n" instead will work.

'n' is a char , response is a string. Strings are arrays of characters. I don't know how you're assigning a value to response , but if you're pulling from an istream, you're going to have to sanitize it before you can use it. You will then either have to use either

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

to compare the entire string to a string of length 1

or

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

to compare one character in the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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