-
July 19th, 2010, 09:00 AM
#1
Linker error. Why?
Hi
My code work before. but Just to test it, what happen if I change data members to static data member, can it work (it should in teory) so the class member, WndProc function should able to access its own data member (as this class should only have one and only object).
FROM
Code:
class MainWnd
{
protected:
WNDCLASS m_wndclass; // This WNDCLASS object
HWND m_hwnd // This Window Handle
public:
MainWnd(HINSTANCE hInstance, int iCmdShow);
~MainWnd(void);
static LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
};
I CHANGE IT TO
Code:
class MainWnd
{
protected:
static WNDCLASS m_wndclass; // This WNDCLASS object
static HWND m_hwnd // This Window Handle
public:
MainWnd(HINSTANCE hInstance, int iCmdShow);
~MainWnd(void);
static LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
};
then i got
1>MainWnd.obj : error LNK2001: unresolved external symbol "protected: static struct tagWNDCLASSW MainWnd::m_wndclass" (?m_wndclass@MainWnd@@1UtagWNDCLASSW@@A)
1>MainWnd.obj : error LNK2001: unresolved external symbol "protected: static struct HWND__ * MainWnd::m_hwnd" (?m_hwnd@MainWnd@@1PAUHWND__@@A)
1>C:\Documents and Settings\Irwan\My Documents\Visual Studio 2005\Projects\Learning\Release\GenericWindow.exe : fatal error LNK1120: 2 unresolved externals
I clean and start debugging, still the same error. Why?
-
July 19th, 2010, 09:36 AM
#2
Re: Linker error. Why?
Static class member variables must be declared and defined. See your favorite C++ book on how to properly declare and define static class member variables.
Code:
class foo
{
static int x;
};
Code:
// In one module:
int foo::x = 0; // or no value, or some other value
Regards,
Paul McKenzie
-
July 19th, 2010, 10:06 AM
#3
Re: Linker error. Why?
Duhh...
Thanks mate. Next time I try to double check on the basis of the language rather than really hard on experimenting with API.
Thanks
-
July 19th, 2010, 10:12 AM
#4
Re: Linker error. Why?
Code:
class MainWnd
{
protected:
static WNDCLASS m_wndclass; // This WNDCLASS object
static HWND m_hwnd // This Window Handle
what happen if I change data members to static data member
If you change the window class and handle members to static, you're never able to create more than a single class instance simultaneously without losing critical information. Static member is shared among all the instances, and every following window creation is going to overwrite previously registered data.
Best regards,
Igor
-
July 19th, 2010, 10:18 AM
#5
Re: Linker error. Why?
I thinks it will be ok as i plan to use this style for class that have one instance
-
July 19th, 2010, 10:39 AM
#6
Re: Linker error. Why?
What are other reasons to have those ones static but bare experimenting?
Best regards,
Igor
-
July 19th, 2010, 10:59 AM
#7
Re: Linker error. Why?
easy accesing class data member while you in MainWnd::WnProc() I guess. Its not going to be a total solution for other type child window, but knowing is gooddddd. It let you sleep better tonight
-
July 19th, 2010, 11:20 AM
#8
Re: Linker error. Why?
easy accesing class data member while you in MainWnd::WnProc() I guess
In fact, people do that kind of things by storing this pointer in window property.
Code:
LRESULT CALLBACK MainWnd::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
MainWnd* pThis = (MainWnd*)GetProp(hwnd, TEXT("This pointer"));
switch (iMsg)
{
case WM_CREATE:
LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
SetProp(hwnd, TEXT("This pointer"), (HANDLE)lpcs->lpCreateParams);
.....
case WM_WHATEVERYOUNEED:
return pThis->OnWhateverYouNeed(wParam, lParam);
.....
}
}
Of course, you have to pass this pointer as lpParam while creating window.
Last edited by Igor Vartanov; July 19th, 2010 at 11:24 AM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|