-
Problem with classes
Im still learning classes, so could someone please tell me why i get this error...
Code:
Error E2235 wndmain.cpp 14: Member function must be called or its address taken in function wndmain::wndmain()
Heres the files...
wndmain.cpp
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "wndmain.h"
wndmain::wndmain()
{
WNDCLASSEX WndClassEx;
hIconMain = LoadIcon(NULL, IDI_APPLICATION);
hCursorArrow = LoadCursor(NULL, IDC_ARROW);
WndClassEx.cbSize = sizeof(WNDCLASSEX);
WndClassEx.lpfnWndProc = WndProc;
WndClassEx.style = CS_HREDRAW | CS_VREDRAW;
return;
}
wndmain::~wndmain()
{
DestroyIcon(hIconMain);
DestroyCursor(hCursorArrow);
return;
}
LRESULT CALLBACK wndmain::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return(0);
}
wndmain.h
Code:
class wndmain
{
public:
wndmain();
~wndmain();
protected:
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static HICON hIconMain;
static HCURSOR hCursorArrow;
};
The error is with this line in wndmain.cpp...
Code:
WndClassEx.lpfnWndProc = WndProc;
Anything would be helpful.
-
2 solutions:
1. make the callback function static and virtually inherit the class to extend it.
2. don't contain the function, in other words have a function pointer.
-
How do i do either of those?
-
1. add static and virtual specifiers to the declaration of the callback, then derive a class using virtual functions
2. add a member specifying the function pointer to a LRESULT CALLBACK (*proc)(HWND, UINT , WPARAM , LPARAM ); and use it for WNDCLASSEX. you could initialize this to a default proc in the constructor or have it passed there, or/and use accessors.
-
I spent the last little while trying to do either of those, and i realized, i have no idea what to do. I have a pretty good understanding of c and c++, just not when it comes to pointers and inheritance (dont even know what that is). Could you show an example please. Thanks.
-
Inheritance is a very very important concept in object oriented programming, if you learn it now, you'll have benefit of it the rest of your life, have a look at Sam's teach yourself C++ in 21 days and pick up what you missed.