invalid operands to binary error | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 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

2nd Nov 2020, 3:19 PM
Sumer Fattoum
Sumer Fattoum - avatar
3 Respuestas
+ 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
2nd Nov 2020, 3:30 PM
Arsenic
Arsenic - avatar
+ 1
nice , thank you for the fix
2nd Nov 2020, 5:15 PM
Sumer Fattoum
Sumer Fattoum - avatar
- 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.
3rd Nov 2020, 11:13 AM
Aditya rout
Aditya rout - avatar