如何使用 C++ 显示变量字符串和PDCurses?
我非常抱歉发布这样一个令人尴尬的新问题,但自从我大学时代以来,我就没有对 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*' to
char*'" 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将
pvers
声明为:此
const
意味着您不会修改pvers
指向的内存。这实际上更像是一个提示,这样如果你打破了这个假设,编译器就会对你大喊大叫。 (这就是为什么你收到编译器警告的原因。)如果你在更改vers
超出此行后使用pvers
,你可能会开始看到一些奇怪的东西,但是对于你发布的代码片段,我没有看到那个问题。Just declare
pvers
as:This
const
means you aren't going to modify the memory pointed to bypvers
. 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 usepvers
after changingvers
beyond this line, but for the snippet you posted I don't see that problem.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.