Public Group

# std::string problem

This topic is 4525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been trying to get to grips with std::string. I have written the code:
#include <string>

int main()
{
std::string blah = "abc" + "def";
return 0;
}


but it won't compile. I get an error saying something about adding two pointers together, and I have no idea why. Any ideas?

##### Share on other sites
In your code example, "abc" is a string litteral and "def" is also a string litteral. You can't add string litterals like this - it doesn't work this way. You can add string objects (i order to concatanate them) but not string litterals. You can also add a string litteral to a string object.

What you want to do is
std::string s = std::string("abc") + std::string("def");

Or more simply
std::string s = std::string("abc") + "def";

The second solution works because it first creates a string object using the string litteral, then you add a string litteral to this string object.

HTH,

##### Share on other sites
Also, just to be complete.
You CAN concatanate string litterals, but the syntax is a little different:

std::string blah = "abc" "def";

There is no operator needed for this.
This kind of syntax is very useful, when you keep your string litterals as defines:
#define FIRSTNAME "John"#define LASTNAME "Doe"#define FULLNAME FIRSTNAME " " LASTNAME#define EMAIL FIRSTNAME "." LASTNAME "@gmail.com"

So now, if you ever need to change first name - you only have to do it in one place. All of the rest defines will take it's value.

##### Share on other sites
Quote:
 Original post by Paulius MaruskaSo now, if you ever need to change first name - you only have to do it in one place. All of the rest defines will take it's value.

<ot>This is why defines r0XorZ... ;)</ot>

##### Share on other sites
Quote:
 Original post by dalleboyThis is why defines r0XorZ... ;)

No they don't - you can't put them into namespaces or classes, you can't limit their scope. That is why they are bad. BAD! :)

Anyway, the same thing with const std::string is also possible (and personaly, i'd prefer this method):

const std::string FIRSTNAME ("John");const std::string LASTNAME ("Doe");const std::string FULLNAME (FIRSTNAME + " " + LASTNAME);const std::string EMAIL (FIRSTNAME + "." + LASTNAME + "@gmail.com");

I posted the defines only to show why the concatenation of string litterals are useful.

##### Share on other sites
Of course, in *real* projects you try to avoid putting too many string literals in your code in the first place, instead reading the important text from files or resources :)

Note that the "string literal concatenation" does something rather different. It's a compile-time operation, and *only* works with a literal for *both* parts. The compiler is simply parsing it as if it actually were one literal, and actually generates one literal stored in the executable when it emits the code. The feature is really intended more, AFAIK, to allow you to wrap string literals across lines of source code. But again, avoid string literals in a serious project :)

##### Share on other sites
Quote:
 Original post by ZahlmanOf course, in *real* projects you try to avoid putting too many string literals in your code in the first place, instead reading the important text from files or resources :)

Not 100% true. If you are using something like GNU gettext, then you are putting ALL of your original strings as litterals in source code. *wink* ;)
Otherwise you're right.

##### Share on other sites
Thanks for the help, everyone. Nice to see so many friendly people!

Now I have a new problem. I have a function that calculates two local variables. I want to put these two variables together into a std::string and return it.

e.g.

int x = 3;
int y = 7;

std::string s = x + y;
return s;

I want this to return a string like "37", but it seems to be treating the ints as ASCII values instead. How do I convert the ints into a std::string?

##### Share on other sites
Quote:
 Original post by mike223Thanks for the help, everyone. Nice to see so many friendly people!Now I have a new problem. I have a function that calculates two local variables. I want to put these two variables together into a std::string and return it.e.g.int x = 3;int y = 7;std::string s = x + y;return s;I want this to return a string like "37", but it seems to be treating the ints as ASCII values instead. How do I convert the ints into a std::string?
The easiest way is probably:
std::string s =    boost::lexical_cast<std::string>(x) +    boost::lexical_cast<std::string>(y);
You can also do the same thing 'manually' using std::stringstream (which is more or less what lexical_cast<> does internally).

##### Share on other sites
You need to use std::stringstream (include the sstream header):
std::stringstream stream;stream << 3 << 7;std::string s = stream.str();

1. 1
Rutin
39
2. 2
3. 3
4. 4
5. 5

• 12
• 17
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633358
• Total Posts
3011509
• ### Who's Online (See full list)

There are no registered users currently online

×