+ 1
invalid operands to binary error
in the first code is working, the second i just replaced the name which is declared string with a written string " whatever" , why shows this error ? https://code.sololearn.com/cRa41co1m8IO/?ref=app https://code.sololearn.com/c4hDYgnKwVou/?ref=app
3 Réponses
+ 9
In the first code, you are performing "+" operation between const char* and std::string which is a valid as it is overloaded in "string" class.
In second code you are performing "+" operation between 2 const char* which is an invalid operation in C/C++.
a simple fix is to type cast one of them to std::string 👇
https://code.sololearn.com/cieP70rT0PUP/?ref=app
+ 1
nice , thank you for the fix
- 1
#include <iostream>
#include <string>
using namespace std;
string helloName (string name)
{
return "Hello" + name + "!" ;
}
int main() {
cout<<helloName("any name") ;
return 0;
}
*only put 'cout<< ' before helloname()f.