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

问题描述

我将termux与clang一起使用。我尝试用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函数使用短整数参数(而不是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[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]);

(尽管这只是一个快速修复)。