简体   繁体   English

从“void*”到“void (*)(..)”的无效转换

[英]Invalid conversion from 'void*' to 'void (*)(..)'

I have error我有错误

invalid conversion from 'void*' to 'void ( )(void , u_int8_t*, u_int8_t*, u_int16_t, void*) {aka void ( )(void , unsigned char*, unsigned char*, short unsigned int, void*)}' [-fpermissive]从 'void*' 到 'void ( )(void , u_int8_t*, u_int8_t*, u_int16_t, void*) {aka void ( )(void , unsigned char*, unsigned char*, short unsigned int, void*)} 的无效转换' [-fpermissive]

void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, u_int8_t *, u_int8_t *, u_int16_t, void *), void *userdata)
{
    void (*handle_friendrequest)(void *, u_int8_t *, u_int8_t *, u_int16_t, void *) = (void *)(function);//->error
    callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata);
}

In CI think your code would work, but C++ will not let you implicitly cast a void* into some other pointer type.在 CI 中认为您的代码可以工作,但 C++ 不会让您将void*隐式转换为其他指针类型。 I would do我会做

typedef void (*FunFriend)(void *, u_int8_t *, u_int8_t *, u_int16_t, void *);

.
.
.
FunFriend handle_friendrequest = (FunFriend)function;

Pointer handle_friendrequest has type指针handle_friendrequest有类型

void (*)(void *, u_int8_t *, u_int8_t *, u_int16_t, void *)

Pointer function has type指针function有类型

void (*)(Messenger *, u_int8_t *, u_int8_t *, u_int16_t, void *)

These are two different function pointer types, which are completely incompatible with each other.这是两种不同的函数指针类型,彼此完全不兼容。 You cannot use function to initialize handle_friendrequest , period.您不能使用function来初始化handle_friendrequest ,句点。

It looks like you tried to cast function to void * as a workaround, but this makes no sense whatsoever.看起来您试图将function强制转换为void *作为一种解决方法,但这没有任何意义。 It simply will not compile.它根本不会编译。 It strange to see you ask this question (if I understood it correctly), since the compiler is complaining about a meaningless invalid cast you yourself inserted into the code.看到你问这个问题很奇怪(如果我理解正确的话),因为编译器抱怨你自己插入到代码中的一个毫无意义的无效强制转换。

You have to make sure both function pointers have identical types, including identical parameter lists.您必须确保两个函数指针具有相同的类型,包括相同的参数列表。 That will fix the problem and eliminate the need for any casts.这将解决问题并消除对任何演员的需要。 As long as the parameter lists are different, there is no solution, aside from ugly hacks.只要参数列表不同,除了丑陋的黑客之外,没有解决方案。

It is just as the error message says.就像错误消息所说的那样。 In the expression in your code, you have (void *)(function) .在您的代码表达式中,您有(void *)(function)

That is of type void * .那是void *类型。 However you then assign that to a variable with function-pointer type.但是,您随后将其分配给具有函数指针类型的变量。 In general, it is not possible to convert between function pointers and object pointers, although some implementations may allow it.通常,不可能在函数指针和对象指针之间进行转换,尽管某些实现可能允许这样做。

If Messenger is actually a typedef for void * then you can just remove the (void *) because function and handle_friendrequest would have the same type, so no cast is necessary.如果Messenger实际上是void *的 typedef,那么您可以删除(void *)因为functionhandle_friendrequest将具有相同的类型,因此不需要handle_friendrequest

If Messenger is not void * then your intended code will have undefined behaviour, because the function pointers have different types.如果Messenger不是void *那么您想要的代码将具有未定义的行为,因为函数指针具有不同的类型。 Consider re-doing your design so that the function you are calling actually has the same prototype as the type of the function pointer.考虑重新设计,以便您调用的函数实际上具有与函数指针类型相同的原型。

If you really want to bull on with the undefined behaviour, the code would be:如果你真的想对未定义的行为进行攻击,代码将是:

{
    typedef void HANDLER(void *, uint8_t *, uint8_t *, u_int16_t, void *);
    HANDLER *handle_friendrequest = (HANDLER *)function;
}
void (*handle_friendrequest)(void *, u_int8_t *, u_int8_t *, u_int16_t, void *) =
        (void (*)(void*, u_int8_t*, u_int8_t*, u_int16_t, void*))function;

Of course function must be aware that it may take void* , not Messenger *当然函数必须知道它可能需要void* ,而不是Messenger *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从`void *`到`void(*)(void *)`的无效转换 - Invalid conversion from `void *` to `void (*)(void*)` 从 'void* (*)(int*)' 到 'void* (*)(void*)' 的无效转换 - invalid conversion from 'void* (*)(int*)' to 'void* (*)(void*)' 无效转换从void *到void(*)(void *)[ - fpermissive] - invalid conversion from void* to void(*)(void*)[-fpermissive] 错误:从'void(*)()'到'void(*)()'的转换无效 - 什么? - error: invalid conversion from 'void (*)()' to 'void (*)()' — what? 从'DWORD(*)(void *)'到'DWORD(*)(void *)'的无效转换 - invalid conversion from 'DWORD (*)(void*)' to 'DWORD (*)(void*)' 在OpenCL clCreateContext中从void(*)(...)到void(*)(...)的转换无效 - Invalid conversion from void (*) (…) to void (*) (…) in OpenCL clCreateContext 从“void (*)()”到“void*”的无效转换 (C++) - invalid conversion from 'void (*)()' to 'void*' (C++) 从'void *'到'void *(*)(void *)'c ++的无效转换? - invalid conversion from ‘void*’ to ‘void* (*)(void*)’ c++? 错误:从'void *'到'void *(*)(void *)'的无效转换 - pthreads - error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ - pthreads 错误:从'int(*)(void *)'到'void *(*)(void *)'的转换无效 - error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM