'init_pair' 的调用没有匹配的函数

No matching function for call to 'init_pair'

我使用带有 clang 的 termux。我尝试在 clang 中编译以下代码,但它输出了标题中提到的错误。 这是代码。

#include <ncurses.h>
#include <string>
using namespace std;

int main(int argc, char** argv) {
  initscr();
  start_color();
  init_pair(1,argv[1],argv[2]);
  attron(COLOR_PAIR(1));
  for (int i = 3; i < argc; ++i) {
    printw("%s",argv[i]);
    attroff(COLOR_PAIR(1));
  }
  refresh();
}

实际的错误消息会告诉您问题所在:

> clang -c foo.cc
foo.cc:8:1: error: no matching function for call to 'init_pair'
init_pair(1,argv[1],argv[2]);
^~~~~~~~~
/usr/include/curses.h:648:28: note: candidate function not viable: no known
      conversion from 'char *' to 'short' for 2nd argument; dereference the
      argument with *
extern NCURSES_EXPORT(int) init_pair (NCURSES_PAIRS_T,NCURSES_COLOR_T,NC...
                           ^
1 error generated.

init_pair 函数使用 short-integer 参数(不是 char*)。您可以通过将 char* 转换为整数来使其编译,例如

> diff -u foo.cc.orig foo.cc
--- foo.cc.orig 2020-09-21 17:35:04.000000000 -0400
+++ foo.cc      2020-09-21 17:36:42.000000000 -0400
@@ -1,11 +1,12 @@
 #include <ncurses.h>
+#include <stdlib.h>
 #include <string>
 using namespace std;
 
 int main(int argc, char** argv) {
 initscr();
 start_color();
-init_pair(1,argv[1],argv[2]);
+init_pair(1,atoi(argv[1]),atoi(argv[2]));
 attron(COLOR_PAIR(1));
 for (int i = 3; i < argc; ++i) {
 printw("%s",argv[i]);

(虽然这只是一个快速修复)。