Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

Simple cout error, could someone have a look.

This topic is 4551 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

Hello, I haven't done much C++ for a while but I thought that this would work.
cout << start_t + "\t\t\t\t\t" + start_x;

Where start_t and start_x are defined as float. However I get this error. error C2111: pointer addition requires integral operand I have been working in javascript for a while and I get the feeling that I am missing something stupid. Thanks in advance for any help with, Mark

Share this post


Link to post
Share on other sites
Advertisement
You're tring to add together a floating-point number and a character array. You can only add together two strings, or a string and a character array. However, if you replace + by <<, it will work, since you're inserting a floating-point number in a stream.

Share this post


Link to post
Share on other sites
To expand a little on what ToohrVyk said. In C++ you can represent strings in two ways, the best way IMO is to use the standard string class (std::string), but you can also just use a pointer which points to where in memory the string is.

By default when you have a string, like "\t" it will be a pointer to where the actual string is stored.

Since it's just a pointer you won't be able to add a number to it. What javascript does is, that it calls a function which translates the float to a number and then create one string from the float string and the actual string.

cout is a stream and support streaming all basic types like float, so when you stream a float into cout like this:
cout << start_t;
cout actually convert start_t to a string and then output it.
So this will work:
cout << start_t << "\t\t\t\t\t" << start_x;
Since cout then start by taking start_t, convert it, then it takes the string and then it takes start_x and convert it.

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!