views:

24

answers:

2

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.

+2  A: 

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
Ah, thanks a ton. I'd vote you up but I don't have the reputation.
Ryoshi
+2  A: 

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.

Ryoshi