Jump to content
  • Advertisement
Sign in to follow this  
mike223

std::string problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Paulius Maruska
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.

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

Share this post


Link to post
Share on other sites
Quote:
Original post by dalleboy
<ot>This is why defines r0XorZ... ;)</ot>


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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 :)


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by mike223
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?
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 this post


Link to post
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();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!