C2440无法在C ++ WinApi中将LRESULT转换为WNDPROC [英] C2440 Can't convert LRESULT to WNDPROC in C++ WinApi
问题描述
我试图用WinApi写这个win32程序,我被困住,因为我下面的教程似乎有一个问题。
I'm trying to write this win32 program with WinApi and I'm stuck because the tutorial I'm following seems to have a problem.
MainWindow.h :
MainWindow.h:
class MainWindow
{
public:
MainWindow(HINSTANCE);
~MainWindow(void);
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
// [...]
MainWindow.cpp:
MainWindow.cpp:
MainWindow::MainWindow(HINSTANCE hInstance) : hwnd(0)
{
WNDCLASSEX WndClsEx;
// [...]
WndClsEx.lpfnWndProc = &MainWindow::WndProcedure;
// [...]
}
LRESULT CALLBACK MainWindow::WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// [...]
}
我必须引用MainWindow :: WndProcedure错误,因为我遵循的签名完全符合教程说,但是lpfnWndProc行在构造函数中给出一个编译时错误:
I must be referencing MainWindow::WndProcedure wrong because I'm following the signature exactly as the tutorial says, however the lpfnWndProc line in the constructor gives a compile-time error:
错误C2440: '=':无法从'LRESULT(__stdcall MainWindow :: *)(HWND,UINT,WPARAM,LPARAM)'转换为'WNDPROC'
error C2440: '=' : cannot convert from 'LRESULT (__stdcall MainWindow::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'
推荐答案
替换
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
由
static LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
此指针是函数调用中的一个隐藏参数,通过声明它静态this指针不是一个参数和两个函数的签名匹配。
The this pointer is a hidden parameter in your function call and by declaring it static the this pointer is not a parameter anymore and the signature of the two functions match.
这篇关于C2440无法在C ++ WinApi中将LRESULT转换为WNDPROC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!