glutBitmapString/glutStrokeString 似乎需要 const unsigned char* - 字符串不起作用
Ubuntu 11.04、G++、freeglut 和 GLUT。
我根本不明白这一点。这是我得到的错误:
whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutStrokeString(void*, const unsigned char*)’
如果我尝试 glutBitmapString:
whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutBitmapString(void*, const unsigned char*)’
这是相关的代码(我认为)。
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);
这个答案 告诉我它应该有效,但事实并非如此。
对于那些不想跳的人的引用:(
// 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");
另外,我尝试在其中留下一个像“要渲染的文本”这样的直接字符串。没有骰子。)
whatever.cc:315:64: error: invalid conversion from ‘const char*’ to ‘const unsigned char*’
我很困惑。据我所知,这是我关于SO的第一个问题,所以如果它没有很好地组合在一起,我深表歉意。我将尽我所能提供任何额外信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::string
不会自动转换为char const*
。但是,您可以使用std::string::c_str()
从std::string
中获取char const*
。例如
glutBitmapString(GLUT_BITMAP_HELVETICA_18, ScoreString.c_str());
注意:您链接到的答案并不表示您可以使用
std::string
。它给出的示例 (glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");
) 使用字符串文字,它是char
数组,不是std::string
。请记住,OpenGL 是一个 C 库,而 C 中不存在
std::string
。There is no automatic conversion of an
std::string
to achar const*
. However, you can usestd::string::c_str()
to get thechar const*
out of anstd::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 ofchar
, not anstd::string
.Remember that OpenGL is a C library, and
std::string
doesn't exist in C.