Question

I want to take the OpenGL functionality from the main source file to a separate class.

// Initialize rendering (GLUT and GLEW)
gfxMgr.init(argc, argv);
...
glutReshapeFunc(gfxMgr.resizeWindow);
glutKeyboardFunc(gfxMgr.keyPressed);
glutKeyboardUpFunc(gfxMgr.keyReleased);

The problem started with defining the callback functions within the class' implementation file. I declared them in the header file as static.

static void init(int , char** );
...
static void drawScene();
static void whenIdle();

Then another problem followed. I want to use a non-static boolean fullScreen variable (as declared in the header) in one of the static functions of my implementation file, but the IDE tells me that "a non-static member reference must be relative to a specific object".

void GfxMgr::init(int argc, char** argv)
{
    ...
    if(fullScr) glutFullScreen();
    ...
}

I don't understand the problem and I don't know what to do. I declared the boolean and a few other variables as static but came up with a bunch of unresolved external symbol errors.

Était-ce utile?

La solution

I don't understand the problem

You need to understand what static member functions are. See this tutorial for example. I also recommend the previous one about static member variables.

In short, static member functions don't know anything about instances of the class. Non-static member variables are bound to an instance of the class. Therefore non-static member variables are unavailable to static member functions.

and I don't know what to do.

What you should do depends on what your class and it's functions are supposed to do.

  • If the behaviour of the function is supposed to depend on value of a member variable of an instance, then the function must be non-static and you must call it on the instance.
  • If the function is supposed to only depend on global state of the class rather than an instance, then the variables the function accesses must be part of that global state (static members).

C callbacks cannot be member functions. Therefore they cannot depend on state of an instance (except a global instance, see this tutorial)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top