微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决没有匹配的函数可以调用'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]);

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。