This is code for an OpenGL application i'm making. I have the following code that produces an access violation (notice: i've only posted the code relevant to the problem):
// file: myHeader.h class MyClass{ int classAttribute; public: void myClassFunction(); bool CreateGLWindow(); friend LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); MyClass(); ~MyClass(); } //file: myHeader.cpp bool MyClass::CreateGLWindow(){ WNDCLASS wc; wc.lpfnWndProc = (WNDPROC) WndProc; } void MyClass::myClassFunction(){ // do stuff } MyClass::MyClass(void){ CreateGLWindow(); } //file main.cpp #include myHeader.h MyClass *p; LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { if (p->classAttribute){ p->myClassFunction(); } } int main(){ MyClass obj; p = &obj; BOOL done=FALSE; while(!done) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))// Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); DispatchMessage(&msg); } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if (obj.active) // Program Active? { if (obj.keys[VK_ESCAPE]) { done=TRUE; } else // Not Time To Quit, Update Screen { DrawGLScene(); obj.Swap(); // Swap Buffers (Double Buffering) } } if (obj.keys[VK_F1]) { obj.keys[VK_F1]=FALSE; if (!obj.changeScreenMode()) { return 0; // Quit If Window Was Not Created } } } } } }
What i get is an access violation on the line p->classAttribute
and debugging shows that the attributes and methods of p can't be evaluated.
Any help will be greatly appreciated.
p
might not be valid by the time the call happens (in fact it is not valid... access violation), so we need to see what you are doing before that call. – Dennis;
after your class, but I take that's a copy/paste/typing error. :) – Xeo