string concatenation

G

Gernot Frisch

Hi,

std::string a("a"), b("b");

This doesn't seem to work, due to a missing '+' operator in
std::string.
printf("%s", (a+b).c_str());

How do you handle such situations?
std::string tmp(a); tmp+=b; ??

Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
K

Karl Heinz Buchegger

Gernot said:
Hi,

std::string a("a"), b("b");

This doesn't seem to work, due to a missing '+' operator in
std::string.
printf("%s", (a+b).c_str());

What are you talking about? The above is done numerous
times all over the world each day.

Did you again forget to #include <string> ?
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

This doesn't seem to work, due to a missing '+' operator in
std::string.
printf("%s", (a+b).c_str());

The operator+ is *not* missing in std::string.
In this case, (a+b) returns a new std::string which is a concatenation of
'a' and 'b' and from which you take the address to its internal string data
(which is a "C-style string"). That's how it works, the error must be
lurking elsewhere.
 
G

Gernot Frisch

Karl Heinz Buchegger said:
What are you talking about? The above is done numerous
times all over the world each day.

Did you again forget to #include <string> ?

Oh wow. That was it. The error from before gave me this as well. And
intellisense is too stupid to show the '+' operator, so I was fooled
twice.
Thank you,
Gernot
 
J

Jacek Dziedzic

Gernot said:
Oh wow. That was it. The error from before gave me this as well. And
intellisense is too stupid to show the '+' operator, so I was fooled
twice.

Also be careful to have it included in your .h files, should you
have any. You often might have functions declared in a .h file
that return or accept a string. Forgetting to #include<string>
there leads to a few screenfuls of meaningless error messages
as well, and a different variety.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top