Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;_C++_Function Pointers - Fatal编程技术网

C++ 如何在C++;

C++ 如何在C++;,c++,function-pointers,C++,Function Pointers,条件 我使用的框架具有如下自定义类型: typedef log (*CustomType) ( int timeStamp, const char* data, int dataSize, void* userData, int dataType, int viewId ) 和MyClass初始化方法,如下所示: MyClass_Init (void **output, CustomType video, CustomType audio, void* userData

条件

我使用的框架具有如下自定义类型:

typedef log (*CustomType) (
  int timeStamp,
  const char* data,
  int dataSize,
  void* userData,
  int dataType,
  int viewId
)
和MyClass初始化方法,如下所示:

MyClass_Init (void **output, CustomType video, CustomType audio, void* userData)
问题

我使用了类似于bellow的init方法,但总是收到错误(因为我使用了框架,所以不会显示错误内容)。请告诉我遗漏了什么

CustomType videoInput;
CustomType audioInput;
void *output  = malloc(sizeof(void*);
void *userData = malloc(sizeof(void*));                     
long result = MyClass_Init(&output, videoInput, audioInput, userData);

此代码有许多错误:

  • 不能混合使用函数指针和方法指针。归结起来,方法的
    this
    必须包含在方法调用签名中。因为函数指针不包括
    这个
    指针(它是函数,不是方法指针),所以两者不能匹配

    大多数基于C的API都包含某种参考值(大多数框架调用那些refCon、context或userData),因此您可以创建一个适配器函数来调用您的方法。CustomType参数列表中的
    userData
    参数看起来像是其中之一(请参阅文档以确定)

    您现在可以在将MyClass_Init设置为回调的任何位置提供userData。因此,如果向库提供回调的函数被调用为
    set\u callback(MyCustomType callback,void*userData)
    ,请执行以下操作

      MyClass *obj = new MyClass; // Or however you create your object
      set_callback( MyClassCallbackAdapterFunction, obj );
    
    使用适配器功能,如:

      log MyClassCallbackAdapterFunction( int timeStamp, const char* data, int dataSize, void* userData, int dataType, int viewId )
      {
          MyClass *myThis = (MyClass*) userData;
    
          // Here you can now call myThis->MyClass_Init( ... ) however you want to.
      }
    
  • malloc(sizeof(void*))
    语句看起来好像您误解了返回参数(有些老师也称为“副作用”)。我没有指向您正在使用的任何API/库的文档,但我非常确定您不应该只传入指针大小的缓冲区。要么在堆栈上提供一个指针,在其中返回一个缓冲区,要么提供整个缓冲区(例如数组)及其大小,这就是回调将写入的位置


  • 您的错误输出是什么?