[英]Error converting void(__cdecl MyClass::*)() to void *
I am trying to link to an external library in my QT application. 我正在尝试链接到QT应用程序中的外部库。 The external library has a header file with the following relevant code I'm trying to call: 外部库有一个头文件,其中包含我要调用的以下相关代码:
extern VGRABDEVICE_API bool V_AssignFrameSizeCallback(IGrabChannel* pChannel, void* pFunc);
In the demo C++ program provided, which has no problems compiling, the following relevant code is: 在提供的演示C ++程序中,该程序没有编译问题,下面的相关代码是:
// in main.cpp
void _stdcall MyFrameSizeCallback(T x) {
do_stuff;
}
int main(int argc, char* argv[]) {
IGrabChannel* pChannel0 = something;
V_AssignFrameSizeCallback(pChannel0, MyFrameSizeCallback);
}
I am trying to incorporate this code into my QT application, but getting problems. 我正在尝试将此代码合并到我的QT应用程序中,但出现问题。 In my mainwindow.cpp file: 在我的mainwindow.cpp文件中:
void _stdcall MainWindow::MyFrameSizeCallback(T x) {
do_stuff;
}
void MainWindow::someFunction() {
IGrabChannel* pChannel0 = something;
V_AssignFrameSizeCallback(pChannel0, &MainWindow::MyFrameSizeCallback);
}
The error I'm getting is: 我得到的错误是:
error: C2664: 'bool V_AssignFrameSizeCallback(IGrabChannel *,void *)' :
cannot convert argument 2 from 'void (__cdecl MainWindow::* )(T)' to 'void *'
There is no context in which this conversion is possible
What do I need to do? 我需要做什么? Thanks. 谢谢。
You have two problems. 你有两个问题。 First, void*
is a data pointer, not a function pointer. 首先, void*
是数据指针,而不是函数指针。 According to the C++ standard, casting between the two is not expected to work. 根据C ++标准,预计两者之间的转换不起作用。 Some platforms provide a stronger guarantee... for example Windows GetProcAddress
and *nix dlsym
mix the two. 某些平台提供了更强的保证...例如Windows GetProcAddress
和* nix dlsym
混合使用两者。
Next, your &MainWindow::MyFrameSizeCallback
is not a function pointer, it is a pointer-to- member -function. 接下来,您的&MainWindow::MyFrameSizeCallback
不是函数指针,而是指向成员函数的指针。 Calling it requires a MainWindow
object, which the external library doesn't know anything about. 调用它需要一个MainWindow
对象,外部库对此一无所知。
You need to provide an ordinary function, not a member function, to the library. 您需要向库提供一个普通函数,而不是成员函数。 If you have some way to get ahold of the MainWindow*
object pointer, you can then call its member function to do the real work. 如果可以通过某种方式获取MainWindow*
对象指针,则可以调用其成员函数来完成实际工作。 Sometimes the library provides a "context" parameter which is passed to your callback; 有时,库提供了一个“上下文”参数,该参数传递给您的回调。 that's a great place to put the object pointer. 那是放置对象指针的好地方。 Otherwise, you'll need to store your MainWindow*
in a global variable. 否则,您需要将MainWindow*
存储在全局变量中。 Easy if you have just one, while if you have more than one you might go with std::map<IGrabChannel*, MainWindow*>
. 如果只有一个,则很容易;而如果有多个,则可以使用std::map<IGrabChannel*, MainWindow*>
。
Code: 码:
MainWindow* MainWindow::the_window;
void MainWindow::MyFrameSizeCallback(T x)
{
do_stuff;
}
void _stdcall MyFrameSizeCallbackShim(T x)
{
MainWindow::the_window->MyFrameSizeCallback(x);
}
void MainWindow::someFunction()
{
IGrabChannel* pChannel0 = something;
the_window = this;
V_AssignFrameSizeCallback(pChannel0, &MyFrameSizeCallbackShim);
}
If the parameter x
isn't an IGrabChannel
, change the map datatype and insertion logic accordingly. 如果参数x
不是IGrabChannel
,则相应地更改映射数据类型和插入逻辑。 If the parameter x
isn't some sort of unique predictable identifier, you may be limited to only doing callbacks to one MainWindow
instance. 如果参数x
不是某种唯一的可预测标识符,则您可能会被限制为仅对一个MainWindow
实例进行回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.