开发者

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

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:

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

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

    // [...]

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

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:

error C2440: '=' : cannot convert f开发者_JS百科rom 'LRESULT (__stdcall MainWindow::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'


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.


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜