function display

Hi guys, probably is a stupid question but…is it possible to call the fuction display if it isn’t in the same class of glutDisplayFunc? I mean, is it possible:

class A:


int main(int argc, char **argv)
{
	glutInitWindowSize(30,30);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInit(&argc, argv);
	glutCreateWindow("example");
	glutDisplayFunc(display);
return 0;
}

class B:


static void display(void)
{
do something..
}

Try it and see. I don’t know why you’d introduce that complexity, but any “function pointer” type object taking no args should do (a static method qualifies; an instance method doesn’t). Only question is whether C++ typing will get in your way.

Hi arthas,
I am sure,Its possible. It works.

it doesn’t work to me :frowning: . I tried to declare in the main:


extern void display(display);

I tried to create an header file, but it doesn’t work too… i’m a newbie, but i think that the problem is the definition of the function display. when the program is all in one file, there isn’t the problem, but when I split it, the compiler expect a definition of it, that there isn’t… But I don’t know how to write it! anyone knows how to solve the problem?

Hi arthas,
Can you post what error your getting, its much better if u can post your buildlog file.

in the file main.cpp:


extern void display(void);

int main(int argc, char **argv)
{
	glutInitWindowSize(30,30);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInit(&argc, argv);
	glutCreateWindow("example");
	glutDisplayFunc(display);

return 0;}

and in the file draw.cpp(same path and project):


static void display(void)
{
  .. draws the scene..
}

the build log is:

Linking…
main.obj : error LNK2019: unresolved external symbol “void __cdecl display(void)” (?display@@YAXXZ) referenced in function _main
C:…Program.exe : fatal error LNK1120: 1 unresolved externals

Use glutDisplayFunc(ClassB::display); This is the syntax for referencing static member functions. Of course make sure that the class header is included in main’s source file.

Hi arthas,
The problem is compiler unable to find display function definition. static function should have the definition in file scope. move your display function definition inside the header file.I hope your including the header file,a file which u given display function declaration.

Remove the static keyword.

Hi avis, I’ve included in the file main.cpp, the line

#include "Draw.h" 

and I created, a file Draw.h, that contains the declaration

 static	void display(void);

the result is :
>Compiling…
>main.cpp
>c:…main.cpp(55) : error C2129: static function ‘void display(void)’ declared but not defined
> c:…draw.h(1) : see declaration of ‘display’
so the problem is the definiton… I only need to clean the code, moving the function display in another file, there isn’t a way?

Solved!!! The problem was Static!!! the function declared static is in the scope of the file it is declared/defined in, so only a c++ problem, not opengl