Overloading Functions

Started by
15 comments, last by DevFred 15 years, 1 month ago
ok, i get another error with unresolved external signals if i declare WndProc as a LRESULT, (just so you know, i declared as void, as that's how you declared it in the other post as far as i could see)

1>mWindow.obj : error LNK2019: unresolved external symbol "public: static long __stdcall mWindow::StaticWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?StaticWndProc@mWindow@@SGJPAUHWND__@@IIJ@Z) referenced in function "public: __thiscall mWindow::mWindow(void)" (??0mWindow@@QAE@XZ)
1>mWindow.obj : error LNK2019: unresolved external symbol "public: long __thiscall mWindow::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@mWindow@@QAEJPAUHWND__@@IIJ@Z) referenced in function "long __stdcall StaticWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?StaticWndProc@@YGJPAUHWND__@@IIJ@Z)
1>C:\Users\Alex\Documents\Visual Studio 2008\Projects\Win Basics\Debug\Win Basics.exe : fatal error LNK1120: 2 unresolved externals
Advertisement
Key phrases:
Quote:
LNK2019 ... unresolved external symbol ... mWindow::StaticWndProc

LNK2019 ... unresolved external symbol ... mWindow::WndProc

This means that the linker can't find the implementations of functions you have declared.

You will need to implement mWindow::WndProc, much like in your very first post, except you will now be calling member functions rather than static functions.

You need to qualify "StaticWndProc" as being a member function of Window:
class mWindow{public:    void WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);     static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);}; // <-- don't forget meLRESULT CALLBACK mWindow::StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){   // ...}

If you are pasting code, use [source] and [/source] tags (or code tags for very short snippets) to get syntax highlighting and to preserve indentation.
ok, thanks guys, one last question (I hope)

do i still keep the code in my WndProc, and is this still used when messages are called, or is all that messaging now handled in StaticWndProc()?
LRESULT CALLBACK mWindow::WndProc(HWND hWnd , UINT Msg, WPARAM wParam, LPARAM lParam){	switch(Msg)	{	case WM_DESTROY:		Close();		PostQuitMessage(WM_QUIT);		break;	case WM_CREATE:		break;	case WM_PAINT:		Render();		break;	default:		return DefWindowProc(hWnd, Msg, wParam, lParam);	}	return 0;} 
The purpose of StaticWndProc is to obtain the correct Window instance and then pass the messages to it. Your non-static WndProc should do the rest.
ok, well whereas i have this code : called with the 'first' window message
if (Msg == WM_NCCREATE) {        LPCREATESTRUCT create_struct = reinterpret_cast<LPCREATESTRUCT>(lParam);        void * lpCreateParam = create_struct->lpCreateParams;        mWindow * this_window = reinterpret_cast<mWindow *>(lpCreateParam);        SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this_window));        return this_window->WndProc(hWnd, Msg, wParam, lParam);}

do i need to make one of these for the other messages WM_PAINT , WM_DESTORY Ect.?
It depends. You *have* to respond to WM_NCCREATE in StaticWndProc to set up the pointer so you can retrieve it later. Whether you want to have a non-static WndProc is up to you. The advantage of making it a member function is that it can be made virtual do a derived class could override it (e.g. to handle additional message types or something).

Alternatively you could call the functions you want from StaticWndProc:
LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){    if (LONG_PTR user_data = GetWindowLongPtr(hWnd, GWLP_USERDATA))    {        mWindow* window = reinterpret_cast<mWindow *>(user_data);        switch(Msg)        {        case WM_DESTROY:            window->Close();            PostQuitMessage(WM_QUIT);            break;        case WM_CREATE:            // window->Whatever();            break;        case WM_PAINT:            window->Render();            break;        default:            return DefWindowProc(hWnd, Msg, wParam, lParam);        }    }}

The advantage of doing it this way is that you don't need to define the additional WndProc member function. The two approaches are quite similar, so it is up to you which you want to use.
BTW, don't confuse overloading with overriding ;)

This topic is closed to new replies.

Advertisement