CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2010
    Posts
    34

    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?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    Join Date
    Jun 2010
    Posts
    34

    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

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  5. #5
    Join Date
    Jun 2010
    Posts
    34

    Re: Linker error. Why?

    I thinks it will be ok as i plan to use this style for class that have one instance

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Linker error. Why?

    What are other reasons to have those ones static but bare experimenting?
    Best regards,
    Igor

  7. #7
    Join Date
    Jun 2010
    Posts
    34

    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

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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
  •  





Click Here to Expand Forum to Full Width

Featured