How to repair "invalid operands of types 'const char' to binary 'operator+'?

16,735

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
16,735
jacknad
Author by

jacknad

Electrical Engineer with experience in microprocessor hardware design, ASM, PL/M, C/C++, C#, Android, Linux, Python, and Java. First high school radio design burst into flames during the demo. First software program was FORTRAN on punch cards. Worked in FL, IL, ND, NJ, TX, VA, and WA.

Updated on June 05, 2022

Comments

  • jacknad
    jacknad almost 2 years

    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+'

  • Michael Krelin - hacker
    Michael Krelin - hacker over 11 years
    +1 but see my comment for the case like this.
  • Bojangles
    Bojangles over 11 years
    Forgive my ignorance, but could you do std::string sql = /* String literals */?
  • chris
    chris over 11 years
    @MichaelKrelin-hacker, Yes, very good point. It's often the case that there's a char * variable as one of the first two arguments, in which case you would need to explicitly convert one, but that doesn't apply here.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 11 years
    @chris, yes that's why I say for the case like this. In particular, for sql I'd prefer parametrized query ;)
  • Michael Krelin - hacker
    Michael Krelin - hacker over 11 years
    How's throwing in heckuvalotta resources "better"?
  • bames53
    bames53 over 11 years
    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<std::string>(myFoo)
  • chris
    chris over 11 years
    @jacknad, I'd really consider accepting Michael's answer. It applies to your situation much better than this does since they're all literals.
  • jacknad
    jacknad over 11 years
    Yes. This works for a set of const strings which I will use in this case but I probably also need what Chris said for the insert into m_table case which has variables and which I neglected to mention.
  • chris
    chris over 11 years
    @jacknad, As long as those variables are std::strings, you can do something like string s = "abc" "def" + otherString + "ghi";
  • jacknad
    jacknad over 11 years
    Haven't touched C++ in a while. Between C, Java, C#, Vala, Python, etc., my head is spinning. But each has its own charm.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 11 years
    Yes, that + consider parametrized queries.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 11 years
    c++ is the most charming! ;-)
  • mohaps
    mohaps over 11 years
    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.
  • Matthieu M.
    Matthieu M. over 11 years
    @mohaps: well, I would avoid using the pesky implementation-defined wide characters; best way not to have portable programs.
  • mohaps
    mohaps over 11 years
    @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.
  • Matthieu M.
    Matthieu M. over 11 years
    @mohaps: well, programming only for the linux world, they are pretty rare since it's utf-8 everywhere :D
  • chris
    chris over 11 years
    There's still std::towstring by the way.