0

So, I am initially trying to do some basic OpenGL (freeglut) stuff in C++, but I've hit a snag. I am getting the following error:

.../TextureMapper.cpp: In member function 'void TextureMapper::run()':
.../TextureMapper.cpp:67:28: error: cannot convert 'TextureMapper::display' from type 'void* (TextureMapper::)()' to type 'void (*)()'  
        glutDisplayFunc(display);
                              ^    

I think that I need display to be a function pointer (or whatever it is called, I'm not terribly familiar with that concept). However, I am doing it wrong. I've never used a function pointer before, and unfortunately the error message means nothing to me, and I can't understand the documentation that I have read. I'm a Java programmer, so it's all very foreign to me.

Here is the declaration for the member function in TextureMapper.h:

void* display(void);

And the instantiation (can't remember what this is called either) in TextureMapper.cpp:

void* TextureMapper::display(void)
{
    ...
}

I noticed that CLion was giving me a warning about this type, but I also tried this first:

void display();

But it gave a similar error.

Here is where I am trying to call glutDisplayFunc:

void TextureMapper::run()
{
    /* Run GL */
    glutDisplayFunc(display);
    glutMainLoop();
}
Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
  • `TextureMapper` is a class. `display` and `run` are member functions inside of it. – Cache Staheli Sep 15 '16 at 02:17
  • Note that `void display();` has the correct signature. You want a function without return value (i.e. a `void(*)()` function pointer) but `void* display();` would return a `void*`. –  Sep 15 '16 at 02:40

1 Answers1

3

glutDisplayFunc expects a C function pointer. This pointer cannot come from a non static member function, because, in C++, member functions are mangled.

So, to be able to make this work, one solution is to define your TextureMapper::Display as a static member function.

So, try something like this:

class TextureMapper
{
public:
    static void display(void);
    ....
};

and in texturemapper.cpp

void TextureMapper::display(void) { \* implementation here *\}
Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • Pointed me in the right direction. I also needed to use variables stored in `TextureMapper` to know what to draw, so I had to consult the linked duplicate question: http://stackoverflow.com/questions/3589422/using-opengl-glutdisplayfunc-within-class – Cache Staheli Sep 15 '16 at 15:18