如何使用 C++ 显示变量字符串和PDCurses?

发布于 2024-08-19 19:11:02 字数 577 浏览 2 评论 0原文

我非常抱歉发布这样一个令人尴尬的新问题,但自从我大学时代以来,我就没有对 C++ 进行过多的研究,我想在某些时候我把我所知道的关于指针和 C++ 字符串的所有知识都从我的脑海中喝了出来。基本上,我正在创建一个 C++ 控制台应用程序(准确地说,是一个 Roguelike),并使用 PDCurses 来处理输出。我想显示动态字符串(我认为这在动态游戏中非常有用,呵呵)但 mvaddstr() 不断向我抛出错误。这是我想要做的一个示例:

 string vers = "v. ";
 vers += maj_vers;// + 48;
 vers += ".";
 vers += min_vers;// + 48;
 vers += ".";
 vers += patch_vers;// + 48;
 char *pvers = vers.c_str();
 mvaddstr(5,17, pvers);
 refresh();

当然,这给我带来了 char *pvers 定义上的“从 const char*' 到char*' 的无效转换”错误。我知道我在这里做了一些非常厚颜无耻、愚蠢的错误,但我对此真的很生疏。任何帮助都会非常有帮助。

I'm extremely sorry to post such an embarrassingly newbish question, but I haven't mucked around much with C++ since my college days and I think at some point I drank all that I knew about pointers and C++ strings right out of my head. Basically, I'm creating a C++ console app (a roguelike, to be precise) with PDCurses to handle output. I want to display dynamic strings (something that I figure would be pretty useful in a dynamic game, heh) but mvaddstr() keeps throwing me errors. Here's an example of what I'm trying to do:

 string vers = "v. ";
 vers += maj_vers;// + 48;
 vers += ".";
 vers += min_vers;// + 48;
 vers += ".";
 vers += patch_vers;// + 48;
 char *pvers = vers.c_str();
 mvaddstr(5,17, pvers);
 refresh();

Of course, this gives me an "Invalid conversion from const char*' tochar*'" error on the char *pvers definition. I know I'm doing something really brazenly, stupidly wrong here but I'm really rusty on this. Any help would be super helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云归处 2024-08-26 19:11:02

只需将 pvers 声明为:

const char *pvers = vers.c_str();

const 意味着您不会修改 pvers 指向的内存。这实际上更像是一个提示,这样如果你打破了这个假设,编译器就会对你大喊大叫。 (这就是为什么你收到编译器警告的原因。)如果你在更改 vers 超出此行后使用 pvers ,你可能会开始看到一些奇怪的东西,但是对于你发布的代码片段,我没有看到那个问题。

Just declare pvers as:

const char *pvers = vers.c_str();

This const means you aren't going to modify the memory pointed to by pvers. It's really more of a hint so that the compiler can yell at you if you break this assumption. (Which is why you got the compiler warning.) You might start to see something funky if you use pvers after changing vers beyond this line, but for the snippet you posted I don't see that problem.

雪花飘飘的天空 2024-08-26 19:11:02

Asveikau 是对的,但我通过搜索一些 ncurses 文档找到了另一个选择 - 我总是可以mvprintw(col, row, "v.%d.%d.%d", maj_vers,min_vers,patch_vers) 具有相同的效果。

Asveikau is right, but I found another option by searching through some ncurses documentation - I could always just mvprintw(col, row, "v. %d.%d.%d", maj_vers,min_vers,patch_vers) for the same effect.

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