4

I'm trying to concat "(" + mouseX + ", " + mouseY ")". However, mouseX and mouseY are ints, so I tried using a stringstream as follows:

std::stringstream pos;
pos << "(" <<  mouseX << ", " << mouseY << ")";
_glutBitmapString(GLUT_BITMAP_HELVETICA_12, pos.str());

And it doesn't seem to work.

I get the following error:

mouse.cpp:75: error: cannot convert std::basic_string<char, std::char_traits<char>, std::allocator<char> >' toconst char*' for argument 2' tovoid _glutBitmapString(void*, const char*)'

What am I doing wrong in this basic string + integer concatenation?

1
  • Can someone who can edit this change the question to "How do I convert a stringstream to a const char*?" Commented Feb 13, 2009 at 1:48

2 Answers 2

5

glutBitmapString() expects a char* and you're sending it a string. use .c_str() on the string like so:

_glutBitmapString(GLUT_BITMAP_HELVETICA_12, pos.str().c_str());
Sign up to request clarification or add additional context in comments.

2 Comments

pedantic, but it's really expecting a "const char *". If it wanted "char *", .c_str() wouldn't work without a const_cast<char *>
@Tom: the prototype asks for a const char *, so I am not sure why that needed to be mentioned.
3

Try

_glutBitmapString(GLUT_BITMAP_HELVETICA_12, pos.str().c_str());

1 Comment

@Evan Teran, I think the downvote came because before the edit he had a slight mistype? I upvoted though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.