3

Ubuntu 11.04, G++, freeglut and GLUT.

I don't understand this at all. Here's the error I get:

whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutStrokeString(void*, const unsigned char*)’

and if I try glutBitmapString:

whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutBitmapString(void*, const unsigned char*)’

Here's the relevant code (I think).

scoreStream << "Score: " << score << "\0";
scoreString = scoreStream.str();

// ...in another method:

glRasterPos2i(0, 0);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString);

This answer tells me it should work, but it just doesn't.

Quote for those who don't want to jump:

// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");

(Also, I have tried leaving a direct string like "text to render" in there. No dice.)

whatever.cc:315:64: error: invalid conversion from ‘const char*’ to ‘const unsigned char*’

I am confused. This is my first question on SO, as far as I remember, so apologies if it isn't too well put together. I'll provide any extra information I can.

Community
  • 1
  • 1
PROGRAM_IX
  • 404
  • 1
  • 5
  • 21

1 Answers1

5

There is no automatic conversion of an std::string to a char const*. However, you can use std::string::c_str() to get the char const* out of an std::string.

e.g. glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString.c_str());

Note: The answer you linked to doesn't say that you can use std::string. The example it gives (glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");) uses a string literal, which is an array of char, not an std::string.

Remember that OpenGL is a C library, and std::string doesn't exist in C.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • I'll try that, thanks! Edit: Nope, still get this error. It wants const _unsigned_ char* for some reason. whatever.cc:315:67: error: invalid conversion from ‘const char*’ to ‘const unsigned char*’ – PROGRAM_IX Jun 27 '11 at 23:01
  • 3
    Just cast it: `(const unsigned char*)scoreString.c_str()` – Peter Alexander Jun 27 '11 at 23:05
  • I see what you mean about using C++ things with a C library. It's my first OpenGL thing on my own, so I'm pretty inexperienced. – PROGRAM_IX Jun 27 '11 at 23:07
  • Casting! Dammit, this is the first time I *haven't* tried casting. Sigh. Thanks a lot, anyway. You've saved me considerable pain there. :) – PROGRAM_IX Jun 27 '11 at 23:09
  • 1
    This is one of those rare times where "casting to shut up the compiler" is the correct thing to do. But most of the time, don't do that (*especially* not with function pointers). – Adam Rosenfield Jun 28 '11 at 02:43