c++ - Window Class Registration Failed -


i writing beginnings of game win32 application. worked fine when registered class within main.cpp, i'm trying move game class make code easier me use.

since have moved code game class, window class registration fails. here code, please note functions empty because haven't got far yet.

getlasterror() returns 87.

main.cpp

#include "main.h"  // entry point program, see game explanation of parameters int winapi winmain(hinstance hinstance,                    hinstance hprevinstance,                    lpstr lpcmdline,                    int ncmdshow) {      game = std::unique_ptr<game>(new game(hinstance, hprevinstance, lpcmdline, ncmdshow));     game->init();      // main game loop     while(game->isrunning())     {         game->handlemessages();         game->update(0.0f);         game->render();     }      return exit_success; } 

game.cpp

#include "game.h" #include <windows.h>  lpcwstr g_szclassname = l"life simulator window class";  game::game(hinstance _hinstance,                        // handle instance of application             hinstance _hprevinstance,               // handle previous instance of application             lpstr _lpcmdline,                       // command line parameters             int _ncmdshow)                          // controls how window show) {     hinstance = _hinstance;     hprevinstance = _hprevinstance;     lpcmdline = _lpcmdline;     ncmdshow = _ncmdshow;     return; }  game::~game(void) { }  bool game::init() {     // set paramaters window class     wc.cbclsextra = 0;                              // number of bytes allocate after window class, not needed showing verbosity     wc.cbsize = sizeof(wndclassex);                 // stores size of wndclassex structure, helping future proof application in case new fields added     wc.cbwndextra = 0;                              // similar cbclsextra, refers window rather window class     wc.hbrbackground = (hbrush) (color_window);     // handle background brush, in case it's colour cast brush handle     wc.hcursor = loadcursor(null, idc_arrow);       // handle cursor, first paramater handle instance of application containing cursor (not needed in case), second resource identifier     wc.hicon = loadicon(null, idi_application);     // similar hcursor, application icon instead     wc.hiconsm = loadicon(null, idi_application);   // above, smaller version of icon     wc.hinstance = hinstance;                       // handle instance of application contains window procedure     wc.lpfnwndproc = game::wndproc;                 // pointer window procedure     wc.lpszclassname = g_szclassname;               // window class name (see global variables)     wc.lpszmenuname = null;                         // specifies resource name of menu, isn't used in case     wc.style = cs_hredraw | cs_vredraw;             // style window class, in case means redraw if it's affected (i.e. resized or moved) vertically or horizontally      // register window class     if(!registerclassex(&wc))     {         // code executed if window class fails register          messagebox(null,                                // owner message box can specified here             l"window class registation failed.",        // message displayed             l"fatal error",                             // title of message box             mb_iconexclamation | mb_ok);                // type of message box, in case has exclamation icon , ok button          return exit_failure;                            // return exit_failure indicate program closed due runtime error     }      // create window     hwnd = createwindowex(ws_ex_overlappedwindow,   // extended window style         g_szclassname,                              // class of window created (this window class created earlier)         l"life simulator",                              // title of window         ws_overlappedwindow,                            // window style         cw_usedefault,                                  // x position of window, here default values used         cw_usedefault,                                  // above, y position         wndwidth,                                   // width of window         wndheight,                                  // height of window         null,                                           // parent window, if has 1         null,                                           // handle menu window         hinstance,                                      // handle instance of application         null);                                          // lpparam can passed on here      if(hwnd == null)     {         // code executed if creating window failed         messagebox(null,                                // owner message box can specified here             l"window creation failed.",                 // message displayed             l"fatal error",                             // title of message box             mb_iconexclamation | mb_ok);                // type of message box, in case has exclamation icon , ok button          return exit_failure;                            // return exit_failure indicate program closed due runtime error     }      showwindow(hwnd,                                    // handle window shown         ncmdshow);                                      // passed on winmain, controls how window should shown (i.e. minimised or maximised)      updatewindow(hwnd);                                 // forces window updated forcing wm_paint message past application queue }  // window procedure game lresult callback game::wndproc(hwnd hwnd,                                // handle window                          uint msg,                      // message processed                          wparam wparam,                 // additional message information                          lparam lparam)                 // more additional message information {     switch(msg)     {     case wm_close:                                      // red x has been clicked         destroywindow(hwnd);                            // sends wm_destroy window         break;     case wm_destroy:                                    // part of program has requested window destroyed         postquitmessage(0);                             // sends quit message window         break;     default:                                            // unhandled messages         return defwindowproc(hwnd, msg, wparam, lparam);// windows handle messages haven't been handled explicitly     }     return 0; }  void game::handlemessages() {     while(peekmessage(&msg,                     // container message             null,                                   // when multiple windows used, can specify 1 here             0,                                      // used filter messages, not needed here             0,                                      // above             pm_remove))                             // remove messages after they've been processed     {             translatemessage(&msg);                 // turns virtual key messages character messages             dispatchmessage(&msg);                  // sends message on window procedure (i.e. wndproc)     }     return; }  void game::update(float elapsedtime) {     return; }  void game::render() {     return; } 

previously working main.cpp

#include <windows.h>  // global variables lpcwstr g_szclassname = l"life simulator window class"; // l casts string wide string , called g_szclassname convention, making code easier read others static const int wndheight = 800; static const int wndwidth = 600;  // window procedure program lresult callback wndproc(hwnd hwnd,                     // handle window                          uint msg,                      // message processed                          wparam wparam,                 // additional message information                          lparam lparam)                 // more additional message information {     switch(msg)     {     case wm_close:                                      // red x has been clicked         destroywindow(hwnd);                            // sends wm_destroy window         break;     case wm_destroy:                                    // part of program has requested window destroyed         postquitmessage(0);                             // sends quit message window         break;     default:                                            // unhandled messages         return defwindowproc(hwnd, msg, wparam, lparam);// windows handle messages haven't been handled explicitly     } }  // entry point program int winapi winmain(hinstance hinstance,                 // handle instance of application                    hinstance hprevinstance,             // handle previous instance of application                    lpstr lpcmdline,                     // command line parameters                    int ncmdshow)                        // controls how window show {     // initialise variables     hwnd hwnd;                                          // handle window     wndclassex wc;                                      // window class container     msg msg;                                            // window message container      // set paramaters window class     wc.cbclsextra = 0;                                  // number of bytes allocate after window class, not needed showing verbosity     wc.cbsize = sizeof(wndclassex);                     // stores size of wndclassex structure, helping future proof application in case new fields added     wc.cbwndextra = 0;                                  // similar cbclsextra, refers window rather window class     wc.hbrbackground = (hbrush) (color_window);         // handle background brush, in case it's colour cast brush handle     wc.hcursor = loadcursor(null, idc_arrow);           // handle cursor, first paramater handle instance of application containing cursor (not needed in case), second resource identifier     wc.hicon = loadicon(null, idi_application);         // similar hcursor, application icon instead     wc.hiconsm = loadicon(null, idi_application);       // above, smaller version of icon     wc.hinstance = hinstance;                           // handle instance of application contains window procedure     wc.lpfnwndproc = wndproc;                           // pointer window procedure     wc.lpszclassname = g_szclassname;                   // window class name (see global variables)     wc.lpszmenuname = null;                             // specifies resource name of menu, isn't used in case     wc.style = cs_hredraw | cs_vredraw;                 // style window class, in case means redraw if it's affected (i.e. resized or moved) vertically or horizontally      // register window class     if(!registerclassex(&wc))     {         // code executed if window class fails register          messagebox(null,                                // owner message box can specified here             l"window class registation failed.",        // message displayed             l"fatal error",                             // title of message box             mb_iconexclamation | mb_ok);                // type of message box, in case has exclamation icon , ok button          return exit_failure;                            // return exit_failure indicate program closed due runtime error     }      // create window     hwnd = createwindowex(ws_ex_overlappedwindow,       // extended window style         g_szclassname,                                  // class of window created (this window class created earlier)         l"life simulator",                              // title of window         ws_overlappedwindow,                            // window style         cw_usedefault,                                  // x position of window, here default values used         cw_usedefault,                                  // above, y position         wndwidth,                                       // width of window         wndheight,                                      // height of window         null,                                           // parent window, if has 1         null,                                           // handle menu window         hinstance,                                      // handle instance of application         null);                                          // lpparam can passed on here      if(hwnd == null)     {         // code executed if creating window failed          messagebox(null,                                // owner message box can specified here             l"window creation failed.",                 // message displayed             l"fatal error",                             // title of message box             mb_iconexclamation | mb_ok);                // type of message box, in case has exclamation icon , ok button          return exit_failure;                            // return exit_failure indicate program closed due runtime error     }      showwindow(hwnd,                                    // handle window shown         ncmdshow);                                      // passed on winmain, controls how window should shown (i.e. minimised or maximised)      updatewindow(hwnd);                                 // forces window updated forcing wm_paint message past application queue      // message loop     while(true){ // program closes instantly otherwise         while(peekmessage(&msg,                             // container message             null,                                           // when multiple windows used, can specify 1 here             0,                                              // used filter messages, not needed here             0,                                              // above             pm_remove))                                     // remove messages after they've been processed         {             translatemessage(&msg);                         // turns virtual key messages character messages             dispatchmessage(&msg);                          // sends message on window procedure (i.e. wndproc)         }     }      return msg.wparam;                                  // contains exit code last message, wm_quit  } 

game.h

#pragma once #include <windows.h>  class game { public:     game(hinstance hinstance,                               // handle instance of application         hinstance hprevinstance,                            // handle previous instance of application         lpstr lpcmdline,                                    // command line parameters         int ncmdshow);                                      // controls how window show      ~game(void);      bool init();      bool isrunning(){return isrunning;}      // window procedure game     static lresult callback wndproc(hwnd hwnd,              // handle window                          uint msg,                          // message processed                          wparam wparam,                     // additional message information                          lparam lparam);                    // more additional message information      void handlemessages();                                  // messages translated , dispatched here      void update(float elapsedtime);                         // game logic      void render();                                          // display results  public: // changed public until can working     bool isrunning;      hinstance hinstance;     hinstance hprevinstance;     lpstr lpcmdline;     int ncmdshow;      lpcwstr g_szclassname;                                  // l casts string wide string , called g_szclassname convention, making code easier read others     static const int wndheight = 600;                       // window height     static const int wndwidth = 800;                        // window width     hwnd hwnd;                                              // handle window     wndclassex wc;                                          // window class container     msg msg;                                                // window message container }; 

the problem have g_szclassname declared both member variable , global variable. member variable g_szclassname not initialized anywhere , can point anything.

there no reason have wc , msg declared member variables not need persist throughout lifetime of object. make them local variables instead.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -