What is wrong with this code?
string s = getString(); // Return string
if(!strcmp(s, "STRING")) {
// Do something
}
While compiling, I am getting the error like:
error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’|
if ( s == "STRING" ).strcmpis theconst char*version. (You could useif (!strcmp(s.c_str(), "STRING"), but don't).strcmpto see what arguments it accepts? Why do you even usestrcmplike that when you already have anstd::stringthat definesoperator==?(s == "STRING")in this case.std::stringtype overloads comparison to aconst char*to avoid creating astd::stringtemporary as your suggested code does....C++: Wherestring == "foo"means exactly what it says.