I have seen this question asked for other languages, but not C++. Here's what I'm trying to do:
bool a = true;
string s;
s.append("a:" + (a? "true" : "false"));
cout << s << endl;
I get a compiler error stating that I cannot add two pointers. What gives?
s.append(string("a:") + (a? "true" : "false"));
"true"
, "false"
, "a:"
, "b:"
and "c:"
are pointers (raw char arrays, actually). When you add them together with +
(e.g. "a:" + "true"
) , then std::string
is not involved, and it's only std::string
which actually gives +
the meaning of concatenation.
Here's what I do in such situations:
s += "a:" + std::string(a ? "true" : "false");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With