In the following code:
(“GLRenderableObject.cpp”)
#include "GLRenderableObject.h"
bool GLRenderableObject::wasMouseFuncRegistered = false;
GLRenderableObject* GLRenderableObject::objects[256];
int GLRenderableObject::objectCount = 0;
GLRenderableObject::GLRenderableObject()
{
if (!wasMouseFuncRegistered)
{
wasMouseFuncRegistered = true;
objects[objectCount++] = this;
[b]// Here I get an exception described below.[/b]
glutMouseFunc(StaticMouseFunction);
[i]// If comment the above, the same exc appears too[/i]
glutMotionFunc(StaticMotionFunction);
}
}
void GLRenderableObject::StaticMouseFunction(int button, int state, int x, int y)
{
for (int i = 0; i < objectCount; i++)
objects[i]->OnMouseEvent(button, state, x, y);
}
void GLRenderableObject::StaticMotionFunction(int x, int y)
{
for (int i = 0; i < objectCount; i++)
objects[i]->OnMotion(x, y);
}
(“GLRenderableObject.h”)
#pragma once
#include "stdafx.h"
class GLRenderableObject
{
private:
static GLRenderableObject* objects[256];
static int objectCount;
static bool wasMouseFuncRegistered;
static void StaticMouseFunction(int button, int state, int x, int y);
static void StaticMotionFunction(int x, int y);
public:
GLRenderableObject();
virtual void Render() abstract {}
virtual void OnMouseEvent(int button, int state, int x, int y) {};
virtual void OnMotion(int x, int y) {};
};
I get “Unhandled exception at 0x100085fa in BransleysFern.exe: 0xC0000005: Access violation reading location 0x000000b0.” on a line marked above. I tried to convert a static function to a global one but nothing changed.
Please help, I am a beginner.
I use OpenGL 3.1 + Visual Studio 2008 + C++ + glut.