回调宏(QT) [英] CALLBACK macro (QT)
问题描述
我有一个带有这样签名的函数
I've got a function with a signature like this
int iV_SetSampleCallback(pDLLSetSample pSampleCallbackFunction)
pDLLSetSample
在哪里
typedef int (CALLBACK * pDLLSetSample) (struct SampleStruct rawDataSample);
我想将我的成员函数作为回调传递
I want to pass my member function as callback
int sampleCallbackFunction(struct SampleStruct sample);
当我这样称呼
iV_SetSampleCallback(&MainWindow::sampleCallbackFunction);
我遇到了错误
error: C2664: 'int iV_SetSampleCallback(pDLLSetSample)': cannot convert argument 1 from 'int (__thiscall MainWindow::* )(SampleStruct)' to 'pDLLSetSample'
我不明白为什么会这样. CALLBACK
宏是什么? __thiscall
是从哪里来的?我应该如何正确地将回调传递给该函数?
And I can't understand why it happens. What is CALLBACK
macro is? Where is __thiscall
came from? How should I correctly pass my callback to this function?
推荐答案
发生错误是因为您试图将指针传递给非静态类成员函数sampleCallbackFunction
,而不是传递给常规函数的指针.请注意,非静态类成员函数具有显式的this
参数. CALLBACK
宏通常在Windows中使用,通常代表stdcall
调用约定.
Your error is occurring because you are trying to pass a pointer to a non-static class member function sampleCallbackFunction
instead of a pointer to a regular function. Note that non-static class member functions have explicit this
parameter. CALLBACK
macro is often used in Windows and typically stands for stdcall
calling convention.
要解决此错误,您需要
- 将成员回调函数声明为静态
- 将CALLBACK宏添加到成员函数之前,以便具有预期的调用约定(或将CALLBACK扩展为的任何内容)
static int CALLBACK sampleCallbackFunction(struct SampleStruct sample);
这篇关于回调宏(QT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!