windows C2440 无法在 C++ WinApi 中将 LRESULT 转换为 WNDPROC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7318893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:00:41  来源:igfitidea点击:

C2440 Can't convert LRESULT to WNDPROC in C++ WinApi

c++windowswinapi

提问by Ozzah

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.

我正在尝试用 WinApi 编写这个 win32 程序,但我被卡住了,因为我遵循的教程似乎有问题。

MainWindow.h:

主窗口.h:

class MainWindow
{
  public:
    MainWindow(HINSTANCE);
   ~MainWindow(void);

    LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);

    // [...]

MainWindow.cpp:

主窗口.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)
{
  // [...]
}

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:

我一定是引用了 MainWindow::WndProcedure 错误,因为我完全按照教程所说的那样遵循签名,但是构造函数中的 lpfnWndProc 行给出了编译时错误:

error C2440: '=' : cannot convert from 'LRESULT (__stdcall MainWindow::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'

错误 C2440:“=”:无法从“LRESULT (__stdcall MainWindow::*)(HWND,UINT,WPARAM,LPARAM)”转换为“WNDPROC”

回答by

replace

代替

LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);

by

经过

static LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);

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.

this 指针是函数调用中的隐藏参数,通过将其声明为静态 this 指针不再是参数,并且两个函数的签名匹配。

回答by interjay

You can't use a non-static member function as a window procedure. If you declare WndProcedureas staticit should compile. A non-member function would work as well.

您不能将非静态成员函数用作窗口过程。如果你声明WndProcedure作为static它应该编译。非成员函数也可以工作。

Non-static member functions have a different signature than static members. This is because they receive an implicit thisparameter in addition to the explicitly defined parameters.

非静态成员函数与静态成员具有不同的签名。这是因为this除了显式定义的参数之外,它们还接收一个隐式参数。

回答by rodrigo

That's because your WndProcedure function must be either a global function or a staticmember function.

那是因为您的 WndProcedure 函数必须是全局函数或静态成员函数。