Ncurses 用户指针

发布于 2024-08-21 08:22:38 字数 673 浏览 4 评论 0原文

我正在尝试学习 ncurses,并且正在此处阅读精彩指南 ,但 用户指针 中的示例无法编译。当我尝试编译时出现此错误。

menu.cpp:在函数“int main()”中:

menu.cpp:44: 错误:从“void (*)(char*)”到“void*”的转换无效< /code>

menu.cpp:44: 错误:初始化 'int set_item_userptr(ITEM*, void*)' 的参数 2

menu.cpp:70: 错误:从 'void* 的转换无效' 到 'void (*)(char*)'

另外,您可能需要添加 cstdlib 和 cstring,以便使用 strlen 和 calloc 进行编译。

我对 void 指针了解不多,因此非常感谢一些修复示例的帮助。

谢谢

I'm trying to learn ncurses, and I'm reading the terrific guide here, but the example at user pointers does not compile. I get this error when I try to compile.

menu.cpp: In function 'int main()':

menu.cpp:44: error: invalid conversion from 'void (*)(char*)' to 'void*'

menu.cpp:44: error: initializing argument 2 of 'int set_item_userptr(ITEM*, void*)'

menu.cpp:70: error: invalid conversion from 'void*' to 'void (*)(char*)'

Also, you probably need to add cstdlib and cstring for that to compile with strlen and calloc.

I don't know much about void pointers, so some help fixing the example would be very appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-08-28 08:22:38

通过阅读手册页:

#include <menu.h>
int set_item_userptr(ITEM *item, void *userptr);
void *item_userptr(const ITEM *item);

描述

每个菜单项都有一个字段,可用于保存特定于应用程序的数据(即,菜单驱动程序代码将其保留)。这些函数获取并设置该字段。

userptr 是您应提供给 set_item_userptr() 的用户特定数据。如果您不想提供任何数据,则应提供NULL。看起来您正在调用 set_item_userptr() ,并将指向函数的指针作为其第二个参数。不保证您可以在 C 或 C++ 中将函数指针转换为 void * 并可移植地转换回来(C++ 在将 void * 转换为任何其他指针类型)。在不知道您要做什么的情况下,如果您确实需要传递函数指针,则应该将其转换为适当的类型:

int ret = set_item_userptr(item, reinterpret_cast<void *>(ptr));
void (*pf)(char*);
pf = reinterpret_cast<void (*)(char *)>(item_userptr(item));

但不能保证它可以工作。您应该执行其他操作,例如拥有一个包含函数指针的 struct,并将指向该 struct 的指针传递给 set_item_userptr()

From reading the manual page:

#include <menu.h>
int set_item_userptr(ITEM *item, void *userptr);
void *item_userptr(const ITEM *item);

DESCRIPTION

Every menu item has a field that can be used to hold application-specific data (that is, the menu-driver code leaves it alone). These functions get and set that field.

userptr is user-specific data that you should supply to set_item_userptr(). If you don't want to supply any data, you should supply NULL. Looks like you are calling set_item_userptr() with a pointer to a function as its second argument. It is not guaranteed that you can convert a function-pointer to void * and back portably, either in C or in C++ (C++ requires a cast when converting a void * to any other pointer type). Without knowing what you are trying to do, if you really need to pass a function pointer, you should cast it to the appropriate type:

int ret = set_item_userptr(item, reinterpret_cast<void *>(ptr));
void (*pf)(char*);
pf = reinterpret_cast<void (*)(char *)>(item_userptr(item));

but it's not guaranteed to work. You should do something else, like having a struct that contains the function pointer, and passing a pointer to the struct to set_item_userptr().

杯别 2024-08-28 08:22:38

您正在使用 C++ 编译器,因此您需要执行

    set_item_userptr(my_items[i], (void *)func);

以下操作:

    p = (void*)item_userptr(cur);

You are using a C++ compiler, so you will need to do:

    set_item_userptr(my_items[i], (void *)func);

and again,

    p = (void*)item_userptr(cur);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文