Ncurses 用户指针
我正在尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过阅读手册页:
userptr
是您应提供给set_item_userptr()
的用户特定数据。如果您不想提供任何数据,则应提供NULL
。看起来您正在调用set_item_userptr()
,并将指向函数的指针作为其第二个参数。不保证您可以在 C 或 C++ 中将函数指针转换为void *
并可移植地转换回来(C++ 在将void *
转换为任何其他指针类型)。在不知道您要做什么的情况下,如果您确实需要传递函数指针,则应该将其转换为适当的类型:但不能保证它可以工作。您应该执行其他操作,例如拥有一个包含函数指针的
struct
,并将指向该struct
的指针传递给set_item_userptr()
。From reading the manual page:
userptr
is user-specific data that you should supply toset_item_userptr()
. If you don't want to supply any data, you should supplyNULL
. Looks like you are callingset_item_userptr()
with a pointer to a function as its second argument. It is not guaranteed that you can convert a function-pointer tovoid *
and back portably, either in C or in C++ (C++ requires a cast when converting avoid *
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: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 thestruct
toset_item_userptr()
.您正在使用 C++ 编译器,因此您需要执行
以下操作:
You are using a C++ compiler, so you will need to do:
and again,