glutBitmapString/glutStrokeString 似乎需要 const unsigned char* - 字符串不起作用

发布于 2024-11-17 08:06:20 字数 1441 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-11-24 08:06:20

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文