Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat strings and numbers in C++?

Tags:

c++

string

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?

like image 301
KingNestor Avatar asked Mar 02 '23 00:03

KingNestor


1 Answers

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());
like image 131
shoosh Avatar answered Mar 11 '23 11:03

shoosh