Ncurses User Pointer
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
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()
.
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);
精彩评论