84

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?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Andry
  • 16,172
  • 27
  • 138
  • 246
  • 15
    Your code should compile just fine, which means you're not showing the exact code that causes the error (on top of not posting the exact error message). – sbi Nov 29 '10 at 14:32
  • Well it does not work... Probably my fault is that I didn't provide compiler... it's g++ not vc... :) – Andry Nov 29 '10 at 14:38

4 Answers4

155

Your code, as written, works. You’re probably trying to achieve something unrelated, but similar:

std::string c = "hello" + "world";

This doesn’t work because for C++ this seems like you’re trying to add two char pointers. Instead, you need to convert at least one of the char* literals to a std::string. Either you can do what you’ve already posted in the question (as I said, this code will work) or you do the following:

std::string c = std::string("hello") + "world";
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thank you very much... ok it works... I expected this was the problem... but really.... There is not a well provided operator to concat two strings (I mean two char*)????? this is really strange... Is there a reason why standard libraries do not support such a simple thing (I guess there must be one). – Andry Nov 29 '10 at 14:35
  • 3
    char* is a pointer, and cannot be added simply because it requires an allocation of memory. std::strings hide allocation, this is why it is possible to provide an operator+ for them. – Vincent Robert Nov 29 '10 at 14:38
  • 12
    @Andry: The reason this doesn't work is that C++ inherits its string literals from C, which is why they are of the type `const char[]`, rather than `std::string`. In C (and therefore also in C++) arrays decay to pointers very easily, which is why "a"+"b" will invoke the built-in operator that adds two pointers. The result of that (a pointer pointing somewhere into memory) is of course quite useless, but nevertheless this is what we're stuck with. – sbi Nov 29 '10 at 14:39
  • @Vincent Thank you. Well it's a reasonable explaination... – Andry Nov 29 '10 at 15:22
  • 2
    @Andry: reasonable but wrong. Allocation has got nothing to do with this. The explanation given by @sbi is the correct one (apart from the fact that the result of a pointer addition isn’t another pointer – it’s simply impossible to add two pointers, since the result would be meaningless). – Konrad Rudolph Nov 29 '10 at 15:37
  • @Konrad True, operator+ cannot be overriden for (char*,char*) and the compiler does not let you add pointer. If it would be possible, it would still be a bad idea because adding those 2 pointers would require an allocation for the result string. Or it could return an std::string but that would make STL far more intrusive than it should be. – Vincent Robert Nov 29 '10 at 17:29
  • 1
    Additionally, directly adjacent string literals are automatically concatenated by the compiler. `std::string c = "Hello" "world";`, for example, will result in `c == "Helloworld"`. – Fang Jan 20 '15 at 12:18
  • 1
    You can also use string literals: `std;:string c = "hello"s + "world"s;` or just make one of them a `std::string`. – Rakete1111 Oct 08 '16 at 18:23
46
std::string a = "Hello ";
a += "World";
Svisstack
  • 16,203
  • 6
  • 66
  • 100
5

I would do this:

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

Which compiles in VS2008.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
5
std::string a = "Hello ";
std::string b = "World ";
std::string c = a;
c.append(b);
Teodor Pripoae
  • 1,388
  • 1
  • 11
  • 26