开发者

How can I display variable strings using C++ and PDCurses?

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);
 ref开发者_运维问答resh();

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜