Proper headers for C++/OOP OpenGL

Hello all,
Ive been reading through the openGL superbible programming my own code as I go along (using C++/MCVS2008) I decided to Object orient everything for ease of use for myself instead of like the book kinda throwing everything under one file. Man did this [censored] MCVS off. I got all kinds of errors and I believe its becuase of my arrangement of #include statements in my sepearate files. Considering if you have just one file you put all your includes at the top and make sure any headers had #ifndef/#define/#endif thats pretty simple but ive got a strange heirachy now so its not so simple anymore. Well my question is abstractly whats the proper way to include headers when you have multiple files that include each other? Heres the details/example below. Im going to specify the files and whats in them.


glFrame.h
if you read the superbible you know this is one of Richard's
files to encapsulate a camera or object.

object.h //personal file
-Contains the definition of an object class
the object class contains a GLFrame object and other items.

object.cpp
-Contains the definition/initialization of the object class

window.h
-contains the definition/prototype of the functions for window.cpp, also contains some opengl types like GLFloat

window.cpp
contains win32 API main call and the callback procedure
Also contains an object class

now for the headers in each of these files im assuming it would have to be like this. (of course im assuming wrong so im looking for something to correct me)


object.h
#include <string>
#include "../../shared/glframe.h"   // Frame Class
#include "../../shared/math3d.h"
#include <gl/gl.h>
#include <gl/glu.h>

object.cpp
#include <string>
#include "object.h"

window.h
#include <windows.h>
#include <gl/gl.h>

window.cpp
#include "window.h"
#include "object.h"

all my .h files have this structure


#ifndef NAMEOFHEADER
#define NAMEOFHEADER
...body...
#endif

Im getting errors like this


1>c:\users\jake\documents\visualstudioprograms\c++andopenglprograms\shared\glframe.h(207) : error C3861: 'glMultMatrixf': identifier not found
1>c:\users\jake\documents\visualstudioprograms\c++andopenglprograms\shared\glframe.h(211) : error C3861: 'glTranslatef': identifier not found
1>c:\users\jake\documents\visualstudioprograms\c++andopenglprograms\shared\glframe.h(234) : error C3861: 'glMultMatrixf': identifier not found

and this


1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2144: syntax error : 'void' should be preceded by ';'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2146: syntax error : missing ';' before identifier 'glAccum'

So what am I doing wrong here as far as my #include because im just not seeing it.

Thank you.

Usually when I get those sort of errors, it’s because I forgot a trailing semicolon after a structure or class definition in a header file.

K, experimenting after much reading… (Not much documentation out there for openGL development just using <gl\gl.h>, <gl\glu.h> and the Win32API for keys and such instead of glut) Everyone seems to utilize GLUT… seems like WIN32API would be much more powerful. Anyways… this is the basic format I found works for MCVS2008 win32project…

Header files for classes contain all include files
.cpp files only include the header file nothing else
Must add opengl32.lib and glu32.lib to additional dependencies under Linker, input options.

Format for classes would look something like this.


object.h
#include <string>
#include "../../shared/glframe.h"   // Frame Class
#include "../../shared/math3d.h"
#include <gl/gl.h>
#include <gl/glu.h>

object.cpp
#include "object.h"

If this format is followed with all header and .cpp files for an OOP approach to opengl with C++ then you should never get any strange errors because of header files.

I guess if your doing procedural C style and you broke up your code into different (object type) sections then this would apply too?

Anyways didnt want to leave this open… so hopefully this may help someone at some point in time.

Also, seems like if your not including glut.h (which has windows.h, gl.h and glu.h) then you must include windows.h manually when just working with gl.h or glu.h

I never used glut, neither glu… Am I strange? :smiley:

In windows you have always include windows.h before gl.h because gl.h use WINAPI and some other win stuff.
If you want you can use glew*, and you don’t need to worry anymore.
Also please include standard header by last or you can create unexpected dependency.

*http://glew.sourceforge.net/