unable to call wndproc from class

Started by
4 comments, last by jpetrie 16 years, 11 months ago
im working on a win32 game, and im getting an error in while setting up my window class. the error says "argument of type 'LRESULT (sys_c::)(HWND_*,UINT,WPARAM,LPARAM)'does not match'LRESULT(*)(HWND_*,UINT,WPARAM,LPARAM)'" i have made many win32 programs before and the only difference here is that im calling wndproc from a class. are you able to do that or is it something else? here the function to create the window

BOOL setup_c::create_window( int width, int height )
{
      WNDCLASSEX   wc;
      RECT         rect;
      
      DWORD        style;
      DWORD        exstyle;
      
      wc.cbSize                = sizeof( WNDCLASSEX );
      wc.style                 = 0;
      wc.lpfnWndProc           = sys.main_wndproc; 
      wc.cbClsExtra            = 0;
      wc.cbWndExtra            = 0;
      wc.hInstance             = wstate.hInstance;
      wc.hIcon                 = NULL;
      wc.hCursor               = LoadCursor( NULL, IDC_ARROW );
      wc.hbrBackground         = NULL;
      wc.lpszMenuName          = NULL;
      wc.lpszClassName         = wstate.winclass;
      
      if( !RegisterClassEx( &wc ) )
      {
          MessageBox( NULL, "Could Not Register Window Class!", "Error", MB_ICONEXCLAMATION | MB_OK );
          return FALSE;
      }
      
      if( !fullscreen )
      {
          style = WS_OVERLAPPEDWINDOW;
          exstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
      }
            
      rect.left    = 0;
      rect.top     = 0;
      rect.right   = width;
      rect.bottom  = height;
      
      AdjustWindowRectEx( &rect, style, FALSE, exstyle );
      
      width = rect.right - rect.left;
      height = rect.bottom - rect.top;
      
      wstate.hWnd = CreateWindowEx( exstyle, wstate.winclass, "title", WS_CLIPSIBLINGS 
                                    | WS_CLIPCHILDREN | style, 0, 0, 0, 0, NULL, NULL,
                                    wstate.hInstance, NULL );
                                    
      if( wstate.hWnd == NULL )
      {
          MessageBox( NULL, "Could Not Create Window!", "Error", MB_ICONEXCLAMATION | MB_OK );
          return FALSE;
      }
      
      ShowWindow( wstate.hWnd, SW_SHOW );
      UpdateWindow( wstate.hWnd );
      
      if( !setupgl.init_window() );
      {
          return FALSE;
      }
      
      SetForegroundWindow( wstate.hWnd );
      SetFocus( wstate.hWnd );
      
      return TRUE;
}

and heres the wndproc

LRESULT CALLBACK sys_c::main_wndproc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
        switch( msg )
        {
                case WM_SYSCOMMAND:
                {
                     switch( wParam )
                     {
                             case SC_SCREENSAVE:
                             case SC_MONITORPOWER:
                                  return 0;
                     }
                }
                break;
                
                case WM_CLOSE:
                {
                     shutdown();
                     return 0;
                }
                break;
                
                case WM_DESTROY:
                {
                     PostQuitMessage( 0 );
                     return 0;
                }
                break;  
                
                case WM_KEYDOWN:
                {
                     // key press function( key, down )
                     return 0;
                }
                break;
                
                case WM_KEYUP:
                {
                     // key press function( key, up )
                     return 0;
                }
                break;
                
                case WM_SIZE:
                {
                     resize_window();
                     return 0;
                }
                break;
        }
                
        return DefWindowProc( hWnd, msg, wParam, lParam );
       
}

thanks in advance
Advertisement
Creating a Win32 Window Wrapper Class
i skimmed through the article and found this

"Anybody who has tried to wrap window classes knows that you can not initialize the window class with a class method as the window procedure unless it is a static method, so the class also needs a static"

does this mean i must make sys.main_wndproc() static? i tried to make sys.main_wndproc() static and an error came up saying that wndproc cant be defined as a static function.
What is the declaration (in the class definition) of the function you are trying to assign to the window procedure pointer? You cannot use member functions (the types don't match; a member function has an implicit this pointer). If you want to use a method that is part of a class, for whatever reason, that method must be a static method. The error you're getting is likely referring to something else, perhaps you using the static incorrectly or placing static on a non-member function (which has a different meaning). What's the header containing sys_c look like?

Also, it will be much more helpful if you would post actual error messages and the code those messages reference, in the future.
this is the sys class header file

#ifndef WIN_SYS_H#define WIN_SYS_H#include <windows.h>#include "win_input.h"#include "win_state.h"class sys_c{      public:             sys_c( void );             ~sys_c( void );                          LRESULT CALLBACK main_wndproc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );                          void shutdown( void );             void resize_window( void );            private:              };extern sys_c sys;#endif


when i put static here...
            static LRESULT CALLBACK main_wndproc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );


EDIT: wow ok, i just put it there and compiled it to get the same error and it worked... thanks for the help. but can someone explain why wndproc would need to be static just so i fully understand
I did, and so does the linked article.

Non-static member functions have an implicit "this" parameter, because non-static member functions must be associated with a particular instance of the class when actually invoked. Thus, a pointer-to-member-function is not the same as a pointer-to-function (what WndProc is), even if the return type and parameter list appear identical -- note that the pointer-to-member-function looks like, in your case "LRESULT (sys_c::)(HWND_*,UINT,WPARAM,LPARAM)," whereas a pointer to a nonmember function would look like "LRESULT ()(HWND_*,UINT,WPARAM,LPARAM)."

This topic is closed to new replies.

Advertisement