简体   繁体   English

将void(__ cdecl MyClass :: *)()转换为void *时出错

[英]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.

相关问题 如何将void(__thiscall MyClass :: *)(void *)转换为void(__ cdecl *)(void *)指针 - How to convert void (__thiscall MyClass::* )(void *) to void (__cdecl *)(void *) pointer 尝试创建posix线程并从&#39;void *&#39;无效转换为&#39;void *(__attribute __((__ cdecl__))*)(void *)错误 - Trying to create posix thread and get invalid conversion from 'void*' to 'void* (__attribute__((__cdecl__)) *)(void*) error 如何将void(myClass :: *)()转换为void(*)() - How to convert void (myClass::*)() to void (*)() 无法将'void(myClass :: *)()转换为void(*)() - cannot convert ‘void (myClass::*)() to void (*)() 错误C2664:&#39;pthread_create&#39;:无法将参数3从&#39;void *(__ clrcall *)(void *)&#39;转换为&#39;void *(__ cdecl *)(void *) - error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(__clrcall *)(void *)' to 'void *(__cdecl *)(void *) 错误LNK2005:无效__cdecl mongo :: uasserted(…)已定义 - error LNK2005: void __cdecl mongo::uasserted(…) already defined 错误 lnk2001 无法解析的外部符号 public void __cdecl - error lnk2001 unresolved external symbol public void __cdecl __cdecl 和 (void) 是什么意思? - What do __cdecl and (void) mean? 如何将“void (MyClass::*)(int)”转换为“void (*)(int)”? - How to cast “void (MyClass::*)(int)” to “void (*)(int)”? 错误:从&#39;void(FlashWork :: *)(int,siginfo_t *,void *)&#39;转换为&#39;void *(*)(int,siginfo_t *,void *)&#39; - error: converting from ‘void (FlashWork::*)(int, siginfo_t*, void*)’ to ‘void* (*)(int, siginfo_t*, void*)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM