#include <iostream> int main() { const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam; std::cout << message; return 0; }
Why does this code not work? Error returned:
error: invalid operands of types `const char[6]' and `const char[8]' to binary `operator+'
Thanks in advance!
EDIT:
Thanks for all the answers. This is my first time on the site and I am astonished at the number of elaborate explanations in such a short time interval.
Regarding the actual question. How come this works then:
const std::string hello = "Hello"; const std::string message = hello + ", world" + "!";
Is it because ", world" and afterwards "!" get concatenated with variable hello (which is defined)?
In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.
You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time. The C# examples in this article run in the Try.NET inline code runner and playground.
As you know, the best way to concatenate two strings in C programming is by using the strcat() function.
Because in C++, string literals (like "Hello"
are not of type std::string
. They are plain char arrays, or C-style strings.
So for the line const std::string message = "Hello" + ", world" + exclam;
,the types the compiler has to work with are:
const std::string message = const char[6] + const char[8] + std::string;
and given the associativity of +
, the operations it has to perform are:
const std::string message = ((const char[6] + const char[8]) + std::string);
That is, the left-most addition must be evaluated first, and the result passed to the rightmost addition.
So the compiler tries to evaluate const char[6] + const char[8]
. There is no addition defined for arrays. Arrays are implicitly converted to pointers, but this doesn't help the compiler. That just means it ends up with const char* + const char*
, and no addition is defined for pointers either.
At this point, it doesn't know that you want the result to be converted to a std::string
.
However, in your second example:
const std::string hello = "Hello"; const std::string message = hello + ", world" + "!";
it works, because the operations the compiler would see were std::string + const char[8] + const char[2]
. Here, the first addition can be converted to std::string + const char*
, and here the addition operator is defined, and returns a std::string
. So the compiler has successfully figured out the first addition, and since the result was a string, the second addition looks like this: std::string + const char[2]
, and like before, this isn't possible, but the array can be converted to a pointer, and then the compiler is able to find an addition operator that works, again resulting in a std::string
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With