类型值不能用于初始化类型实体

A value of type cannot be used to initialize an entity of type

本文关键字:类型 实体 初始化 不能 用于      更新时间:2023-10-16

我有一个函数:

long __stdcall call_DLL(long n, byte s0, byte s1, long(__stdcall *CallBack)(long m, byte s0, byte s1)){
//trying to copy the address of CallBack to another pointer
long *x = &CallBack;
}

我遇到了一个错误:

a value of type "long(__stdcall *CallBack)(long m, byte s0, byte 
s1)"cannot be used to initialize an entity of type "long *"

有人知道我该怎么做?

如果您真的想保存回调以稍后使用,则可以:

long (* __stdcall x)(long, byte, byte) = CallBack;

或在C 中,您也可以将auto用于简洁:

auto x = CallBack;

在任何一种情况下,以后都使用

long ret = x(n, s0, s1);

否则,如果您只想调用该回调,请执行

之类的事情
long x = CallBack(n, s0, s1);

,而不是用不兼容的long *初始化功能指针,而是用兼容类型初始化它。@algirdaspreidžius

指针函数(长,字节,字节(返回长。

long __stdcall call_DLL(long n, byte s0, byte s1,
    long (__stdcall *CallBack)(long m, byte s0, byte s1)) {
  // long *x = &CallBack;
  long (__stdcall *x)(long, byte, byte) = CallBack;
  // sample usage of `x`
  return x(n, s0, s1);
}
相关文章: