#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 (whi开发者_JS百科ch is defined)?
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
.
"Hello" + ", world"
Since these are c-style strings, you cannot append them with +. You can append a std::string to a c-style string, but not 2 c-style strings this way, instead add a std::string() constructor around one of them to make a temporary, ie:
"Hello" + std::string(", world")
C++ doesn't do many of the automatic 'behind the scenes' conversations of other OO languages.
As Doug said you need to do std::string("hello") + std::string(" world"), the language doesn't do this for you.
However you can do
std::cout << "hello" << "world" << exclam;
Because std::cout knows how to print a const char[] as well as a string
String literals are simply zero terminated array of chars in C++. There is no operator that allows you to add 2 arrays of chars in C++.
There is however a char array and std::string + operator.
Change to:
const std::string message = std::string("Hello") +", world" + exclam;
In some languages like Python string literals are equivalent to variables of type strings. C++ is not such a language.
In the line where you form your message, the entire expression to the right of the = is performed first, and only then it is assigned to a C++ string. At that point, your "Hello" and your ", World" are still C strings (const char[]) which is why you are getting an error. Addition goes from left to right, so the pair of the C strings is added before you attempt to add the combination to the std::string exclam.
You need to either cast them within the expression (e.g., std::string("Hello")), or create string variables for each like you did with Exclam.
The problem is that basic string literals like "this is a literal"
are not of type std::string
so they do not concatenate with the +
operator.
In post C++14
you can use standard user defined string literals that are of type std::string
:
using namespace std::literals; // somewhere in the scope
auto message = "Hello"s + ", world"s; // message is type std::string
See std::literals::string_literals::operator""s.
C-style string literals ("Hello" and ", world") are equivalent to anonymous arrays:
static const char anon1[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
static const char anon2[8] = { ',', ' ', 'w', 'o', 'r', 'l', 'd', '\0' };
...so when you type "Hello" + ", world"
, you're trying to add two arrays anon1 + anon2
which is not an operation that C or C++ supports.
Remember, string literals in C/C++ are just arrays (or addresses of arrays). You have to use a string class (e.g. std:string
) in order to use operators like +
.
精彩评论