[c++] Concatenating strings doesn't work as expected

I know it is a common issue, but looking for references and other material I don't find a clear answer to this question.

Consider the following code:

#include <string>

// ...
// in a method
std::string a = "Hello ";
std::string b = "World";
std::string c = a + b;

The compiler tells me it cannot find an overloaded operator for char[dim].

Does it mean that in the string there is not a + operator?

But in several examples there is a situation like this one. If this is not the correct way to concat more strings, what is the best way?

The answer is


std::string a = "Hello ";
a += "World";

I would do this:

std::string a("Hello ");
std::string b("World");
std::string c = a + b;

Which compiles in VS2008.


std::string a = "Hello ";
std::string b = "World ";
std::string c = a;
c.append(b);

Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to operator-keyword

What does double question mark (??) operator mean in PHP How to use comparison operators like >, =, < on BigDecimal What do >> and << mean in Python? How do operator.itemgetter() and sort() work? ' << ' operator in verilog Overloading operators in typedef structs (c++) C++ Double Address Operator? (&&) Concatenating strings doesn't work as expected

Examples related to string-concatenation

How do I concatenate strings? How do I concatenate strings in Swift? How to set the id attribute of a HTML element dynamically with angularjs (1.x)? C++ String Concatenation operator<< how to use concatenate a fixed string and a variable in Python How do I concatenate strings and variables in PowerShell? Concatenating variables in Bash SQL NVARCHAR and VARCHAR Limits Concatenating string and integer in python PHP string concatenation

Examples related to stdstring

Is it possible to use std::string in a constexpr? Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’ Remove First and Last Character C++ Concatenating strings doesn't work as expected What does string::npos mean in this code? How to replace all occurrences of a character in string? std::string formatting like sprintf convert a char* to std::string How to get the number of characters in a std::string? c++ integer->std::string conversion. Simple function?

Examples related to standard-library

Removing duplicate elements from an array in Swift Why is it OK to return a 'vector' from a function? How to check if a file exists in Go? "Unresolved inclusion" error with Eclipse CDT for C standard library headers Case Insensitive String comp in C Concatenating strings doesn't work as expected Read whole ASCII file into C++ std::string Getting a machine's external IP address with Python