0
votes

So I am in the process of creating a WNDCLASSEX within a method contained in D3DApp class which I am going to be deriving from with another class e.g. Engine, Game, etc... called InitMainWindow (which is also protected in the base class):

bool D3DApp::InitMainWindow() {

    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc; // This is where the error is!
    wc.hInstance = mAppInst;
    wc.hCursor = LoadCursor(mAppInst, IDC_ARROW);
    wc.lpszClassName = L"MainWindow";

    RegisterClassEx(&wc);
    RECT rect = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
    mMainWnd = CreateWindowEx(NULL, L"MainWindow", L"Test", WS_OVERLAPPEDWINDOW, 300, 300, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mAppInst, NULL);
    ShowWindow(mMainWnd, NULL);

    return true;
}

My WNDPROC Callback declaration and definition is within the same D3DApp base class where InitMainWindow() is located, defined as such:

virtual LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

When I am filling out the lpfnWndProc variable within the WNDCLASSEX struct I receive a C2440 error stating the following

'=': cannot convert from 'LRESULT (__cdecl D3DApp::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'

Do I have to create a definition of my WNDPROC Callback in the class deriving from D3DApp? Or do I have to completely remove this callback from the base class and derived class and place it outside and above the scope of my WinMain function? I've been researching for about 2 days on how to fix this error and have had no luck on finding any possible fixes... I think my C++ skills just may not be as excellent as I think they are. But if anyone can help me understand where my problem is that would be amazing!

p.s and yes I know I have some error checking to do but I just want to make sure it works for my compiler first :)

1
You should declare that window procedure as static and remove virtual keyword from it or declare and define it it outside the class as a non-member functionAsesh
That fixed the error I was receiving, thank you for the help. MSDN format of documentation is still very confusing to me to understand and I really appreciate that people like you are willing to help with problems like these. @AseshSharpie
Have added more information in the answer belowAsesh

1 Answers

0
votes

Here's the signature of WNDPROC:

typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);

but your declaration is different:

LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

You should either declare it outside of the class like this:

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

or declare it as static and remove virtual keyword. Also notice, you should add CALLBACK which resolves to __stdcall calling convention as your project is configured to use __cdecl calling convention