I'm playing around with a basic console application, and thought to convert it into a terminal user interface with ncurses. Unfortunately, it doesn't work as I expected. The program is a simple for-loop, where I wish to convert an int to a string. It is possible to do this with cout, but I wish to use ncurse's printw function.
Here's the code:
#include <string>
#include <iostream>
#include <ncurses.h>
using namespace std;
int i;
int main() {
initscr();
for (i = 1; i <= 5; ++i) {
std::string s = std::to_string(i);
char const *pchar = s.c_str(); //use char const* as target type
// cout << s << "\n"; // This works
printw(s); // This doesn't work.
refresh();
}
getch();
endwin;
return 0;
}
Compiler flags:
g++ -o hello hello.cpp -lncurses -std=c++11
Fails to compile with error:
hello.cpp: In function ‘int main()’:
hello.cpp:15:10: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int printw(const char*, ...)’
printw(s); // This doesn't work.
^
As mentioned, the conversion works well with count (which is commented out in above code). Perhaps this is a bug in ncurses, but is there a work around?
Aucun commentaire:
Enregistrer un commentaire