Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid operands to binary expression ('const char*' and 'const char *') [duplicate]

Tags:

c++

I want to know the reason for this error in EASY words please.

#include <iostream>
#include <string>

int main()
{
    std::string exclam = "!";
    std::string message = "Hello" + ", world" + exclam;

    std::cout << message << std::endl;

    return 0;
}

test.cpp:55:35: error: invalid operands to binary expression ('const char *' and 'const char *') std::string message = "Hello" + ", world" + exclam;

like image 928
blue_mango Avatar asked Apr 09 '18 02:04

blue_mango


1 Answers

"Hello" and ", world" aren't strings, they are const char * which as no overload for the + operator.

You would have to do something like this:

std::string message = std::string("Hello") + std::string(", world") + exclam;

like image 84
Loocid Avatar answered Nov 04 '22 19:11

Loocid