Invalid operands

I am having trouble figuring out what the error is in the following code

cout << "There once was a person named " + name;


The error message I receive states the following:

[Error] invalid operands of types 'const char [31]' and 'char [60]' to binary 'operator+'
[Error] invalid operands of types 'const char [31]' and 'char [60]' to binary 'operator+'

No such operator exists for the appension of two operands of those types. So, I suggest you include the string header file from the C++ Standard Library and construct a temporary std::string object to allow for the invocation of this function: http://en.cppreference.com/w/cpp/string/basic_string/operator%2B

std::cout << "There once was a person named " + std::string(name);
Last edited on
In C++, a string literal is converted automatically to const char and since string literals are simply array, there is no overload for the '+' operator. So fix this, do the following:

cout << std::string("There once was a person named ") + name;
Or you could simply do: cout << "There once was a person named " << name;
Thank all of you for your responses - they have been quiet helpful and I have been able to fix my problem
Topic archived. No new replies allowed.