Why doesn't this example work for me?
I am trying to compile the following example using Visual C++:
Code:
/* ---- AbstractWindow.h ---- */
class AbstractWindow {
// --- previous class definition here ---
public:
virtual bool OnCommand (int ctrlId, int notifyCode) { return false; }
virtual bool OnDestroy () { ::PostQuitMessage (0); return false; }
virtual bool OnClose () { return false; }
// --- and so on, add as many handlers as necessary ---
// --- and define their default behavior ---
// --- usually they return 0, but check MSDN for details ---
};
/* ---- AbstractWindow.cpp ---- */
LRESULT CALLBACK AbstractWindow::msgRouter (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
{
AbstractWindow *wnd = 0;
if (message == WM_NCCREATE) {
wnd = (AbstractWindow *) ((LPCREATESTRUCT(lParam))->lpCreateParams);
::SetWindowLong (hwnd, GWL_USERDATA, long(wnd));
wnd->OnNCCreate (/* params */);
} else {
wnd = (AbstractWindow *) (::GetWindowLong (hwnd, GWL_USERDATA));
if (wnd) {
switch (message)
{
case WM_COMMAND:
return OnCommand (LOWORD (wParam), HIWORD(wParam))
case WM_DESTROY:
return OnDestroy ();
case WM_CLOSE:
return OnClose ();
// --- and so on, it'll be a lot of work to do,
// but most apps don't use every window message ---
default: // for the messages you did not define any handlers
return ::DefWindowProc (hwnd, message, wParam, lParam);
}
}
else // for messages that arrive prior to WM_NCCREATE
// and the HWND -> AbstractWindow * association was not made
return ::DefWindowProc (hwnd, message, wParam, lParam);
}
}
but for some reason I keep getting the following errors:
Code:
.\test.cpp(97) : error C2352: 'AbstractWindow::OnCommand' : illegal call of non-static member function
.\test.cpp(43) : see declaration of 'AbstractWindow::OnCommand'
.\test.cpp(99) : error C2352: 'AbstractWindow::OnClose' : illegal call of non-static member function
.\test.cpp(45) : see declaration of 'AbstractWindow::OnClose'
.\test.cpp(101) : error C2352: 'AbstractWindow::OnDestroy' : illegal call of non-static member function
.\test.cpp(44) : see declaration of 'AbstractWindow::OnDestroy'
I'm not sure I understand why these functions have to be static. Here is the full example of the class that I'm forming:
Code:
class AbstractWindow
{
protected:
HWND _hwnd;
LPCSTR _className;
list<HWND> children;
public:
//default constructor
AbstractWindow () { }
//member functions
static LRESULT CALLBACK msgRouter (HWND, UINT, WPARAM, LPARAM);
virtual bool Create (char *, int, int, int, int);
virtual bool OnCommand (int ctrlId, int notifyCode) { return false; }
virtual bool OnDestroy (void) { ::PostQuitMessage (0); return false; }
virtual bool OnClose (void) { DestroyWindow (_hwnd); return false; }
virtual bool OnNCCreate (void) { MessageBox (NULL, "NCCreate", "NCCreate", MB_OK); return false; }
bool AddButton (char *, int, int, int, int, int);
bool AddTextBox (char *, int, int, int, int, int);
bool AddComboBox (char**, int, int, int, int, int);
bool AddStaticText (char *, int, int, int, int, int);
};
bool AbstractWindow::Create (char *name, int x, int y, int width, int height)
{
_hwnd = CreateWindowEx (0, _className, name, WS_OVERLAPPEDWINDOW, x, y, width, height,
NULL, NULL, GetModuleHandle (NULL), this);
if (!_hwnd)
return false;
return true;
}
LRESULT CALLBACK AbstractWindow::msgRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
AbstractWindow *wnd = 0;
if (msg == WM_NCCREATE)
{
wnd = (AbstractWindow *) ((LPCREATESTRUCT) lParam)->lpCreateParams;
::SetWindowLong (hwnd, GWL_USERDATA, long (wnd));
wnd->OnNCCreate ();
}
wnd = (AbstractWindow *) (::GetWindowLong (hwnd, GWL_USERDATA));
if (wnd)
{
switch (msg)
{
case WM_COMMAND:
return OnCommand (LOWORD (wParam), HIWORD (wParam));
case WM_CLOSE:
return OnClose ();
case WM_DESTROY:
return OnDestroy ();
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
}
else
return DefWindowProc (hwnd, msg, wParam, lParam);
}
bool AbstractWindow::AddButton (char *caption, int xPos, int yPos, int width, int height, int id)
{
HWND child = CreateWindowEx (0, "BUTTON", caption, WS_CHILD | WS_VISIBLE, xPos, yPos,
width, height, _hwnd, (HMENU) id, GetModuleHandle (NULL), NULL);
if (child)
{
children.push_front (child);
return true;
}
else
{
DestroyWindow (child);
return false;
}
}
Any help would be GREAT!!! And insight i suppose, cuz i guess i dont quite understand static, as many times as i read about it everywhere i can.