10

Possible Duplicate:
How do I concatenate multiple C++ strings on one line?

According to this a C++ std::string is concatenated by using operator+. Why then does this code

using namespace std;
string sql = "create table m_table(" + 
    "path TEXT," +
    "quality REAL," +
    "found INTEGER);"; 

cause this error?

invalid operands of types 'const char [22]' and 'const char [17]' to binary 'operator+'

Community
  • 1
  • 1
jacknad
  • 13,483
  • 40
  • 124
  • 194
  • 2
    Those aren't C++ string objects you're trying to concatenate, but C strings. String literals are C strings. – Ben Voigt Oct 05 '12 at 18:17

3 Answers3

14

What chris said, but in this particular case you can do

string sql = "create table m_table("
    "path TEXT,"
    "quality REAL,"
    "found INTEGER);"; 

Which will concatenate strings at compile time.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
12

You need to explicitly convert it to a string to match the argument list:

string sql = std::string("create table m_table(") + 
"path TEXT," +
"quality REAL," +
"found INTEGER);"; 

Now the first one is a string matched up with a const char[N], which matches one of the operator+ overloads and returns a new std::string, which is used to repeat the process for the rest.

chris
  • 60,560
  • 13
  • 143
  • 205
1

a better way is to use std::ostringstream

#include <sstream>

const std::string myFunc(const std::string& s1, const std::string& s2)
{
  std::ostringstream os;
  os<<s1<<" "<<s2;
  return os.str();
}

The advantage is that you can use the std::ostream << operator overloads to stringify non string values too

mohaps
  • 1,010
  • 5
  • 10
  • 1
    How's throwing in heckuvalotta resources "better"? – Michael Krelin - hacker Oct 05 '12 at 18:27
  • 1
    It is useful to be able to combine other types into a string, but I don't think there's any need to change simple string concatenation to get that. I think sticking with operator+ and just using `std::to_string()` or similar functions is a better way: `"hello" + std::to_string(50)`. lexical_cast even lets you do this with any type that has an ostream `operator<<`: `"Hello, " + boost::lexical_cast(myFoo)` – bames53 Oct 05 '12 at 18:30
  • Michael, it's better if you need to concatenate just more than strings and/or (by using std::wostringstream) need to handle wide char strings etc. the stringstream classes in STL were provided specifically for these type of usecases, extending the iostreams paradigm. the std::ostream semantics for formatting, precision, width etc. can also come into play here. – mohaps Oct 05 '12 at 20:11
  • @mohaps: well, I would avoid using the pesky implementation-defined wide characters; best way not to have portable programs. – Matthieu M. Oct 05 '12 at 20:13
  • @MatthieuM. Amen to that! :) But in a large enough codebase with pesky little legacy thirdparty libraries... it's better to plan ahead and allow for the dreaded wchar_t stuff. – mohaps Oct 05 '12 at 20:30
  • @mohaps: well, programming only for the linux world, they are pretty rare since it's utf-8 everywhere :D – Matthieu M. Oct 05 '12 at 20:33
  • There's still `std::towstring` by the way. – chris Oct 06 '12 at 16:05