I have a class called cube to keep track of my cubes, corner points and so on. In it I have a public function that is called DisplayCube(). In that function it has glBegin(GL_QUADS) and glEnd() with all the stuff to draw a cube for me. But when I put someCube.DisplayCube() in my RenderScence() I get linker errors for all my functions in my Init.h file...
I am #include-ing init.h in my cube class header file. But init.h has #ifndef... in it so whats going on? Can I only have glBegin(...) in the RenderScence Function? What can I do?
It looks like you're defining and supplying code for global functions inside a header file which is then being included into two source files.
The #ifdef/#define trick only works on headers. As far as the compiler's concerned, it's totally separate for the two source files so it'll happily generate code for the same functions in different object files.
You need to move the code into one source file only and it should sort it
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
So I can't have OGL code in a class, unless I template it. And the code that I need to move to the source would probably be the OGL code in my class gets moved to my RenderScene file (render.h). Would that work?
I didn't understand, I'm not that far so if I should just start over thats fine, I've only got a couple hours of work in it.
Heres a zip file of my project. Its MS VC++, hope that good for you.
NOMAD
(Parksie, Thank You. I know you don't have to do this and I really appreaciate it!)
Create a .cpp files with the same name as each of your .h files. Then, take all of the function definitions (the code, not the prototypes), and stick them into the .cpps. Now you have .h files with only prototypes in them. DONT #include the .cpps into other .cpps or .h files. Just leave them in the project, and compile.
The reason for your errors is fairly simple. Take 2 files:
Just follow the pre-processor. First, it includes x.h. It sees that _FILE1 is not defined, so it continues. But the compiler sees this:
Code:
...
void x()
{
...
}
...
void x()
{
...
}
Get it? Two definitions. The solution is simple. Define the function only once, in an individually compiled .cpp file. Each cpp in your project is compiled into its own .obj file. As long as there is a prototype for a function, you will get this .obj from your cpp. It is the Linker's job to actually resolve all of these .obj files into an exe. So, when it sees that main.obj and init.obj BOTH have void x() defined in them, it yells. What you do is only have void x() defined in one of the cpps, and simply let the other know it is there. We end up with this:
// file main.cpp
#include "init.h" // just the prototype!
int main()
{
x();
return 0;
}
See how it all fits? main.cpp doesnt have an implementation of void x(), but it knows it exists, because of the prototype defined in init.h. init.cpp has the actual implmentation of the function, and the linker will resolve the dependencies between the .obj files.
If it seems a bit complicated, just read it over a few times. It will make sense eventually =). Its easier then it looks.
I apologize for giving up and thowing error messages up for someone else to fix for me! Next time I do that tell me to look harder.
I'm still stuck, I guess I reorganized my project to what you guys said. All functions are in the appropriate cpp file, globals and prototypes are in h files.
I found that when I remove the #include "Init.h" line from my cube class .h file the linking errors go away, but then I get glBegin and the rest of the OGL functions in my class are undeclared.
Is all this from me having my globals in the Init.h file? what can I do, I need those as globals. Where can I put them?
If you want to look at my project to see where I have everything I'll include that too.