Problem with including header files

Started by
12 comments, last by Zeraan 18 years, 9 months ago
ok, I've managed to fix my DirectX engine, so when I include it, it doesn't give errors, and everything's working smoothly now. Now I'm trying to divide the program into different files such as Game_Init, Game_Main, etc. The only way I can see to do that is to declare the D3D class in WinMain.cpp (where the WinMain function is, obiviously) but as a global in that file, then pass it to other functions. I think I've forgotten how to pass it through functions, due to errors that I've recieved. I feel like a noobie again :( I tried it like this:

function header: (in GameMain.h)

bool Game_Init(TGraphic*, HWND, int, int); //TGraphic is D3D engine, two ints is size of screen

function prototype: (in GameInit.cpp)

bool Game_Init(TGraphic* Graphic, HWND hWnd, int width, int height)
{
...
}

function call: (in WinMain.cpp)

if(!Game_Init(Graphic, g_hWnd, 800, 600))
		return 0;
Where am I going wrong? Thanks! *Edit, this problem is resolved, scroll to bottom for more details. [Edited by - Zeraan on July 2, 2005 1:11:57 AM]
Advertisement
Hi Zeraan,

As far as I can see, there's probably nothing wrong with your code. Could you give us the errors you are getting?
never mind, I think I got it right. I removed the * pointers, and it seems to work. But the problem is, I had to include the d3d engine "outpost.h" in both GameMain.h and GameInit.h to get it to work, or have GameInit.h include GameMain.h, but every time I do that, I get 74 errors basically saying that I'm redefining the stuff in D3D that've been already defined. I think I did something wrong in creating the .h files?

one of the errors looks like this:

GameInit.obj : error LNK2005: "public: __thiscall TParticle::TParticle(void)" (??0TParticle@@QAE@XZ) already defined in WinMain.obj

it's the same with every function in all of the engine.

the Outpost.h looks like this:
#ifndef OUTPOST#define OUTPOST#include <d3d8.h>#include <d3dx8.h>#include <dinput.h>#include <dplay8.h>#include <dpaddr.h>#include "OutpostG.h" //Graphic#include "OutpostI.h" //Input#endif


The outpost.h is included in gamemain.h, which is included in Winmain.cpp. So if I include the same file in other .cpp, would that cause problems?

the #ifndef OUTPOST should have taken care of this problem?
For all your variables that are declared in header files, make sure to use the extern keyword to declare them, then declare the variables themselves in another source file. Like so:

// headerextern int whatever;

// source fileint whatever = 0;

// sourcefile 2int main(){    cout << whatever << endl;}
in OutpostG.h and OutpostI.h, theres no variables, except for classes, and those classes are defined in their source files. So I have to add extern to those classes? I'll try it now, correct me if I'm wrong. Thanks for the advice, I'll keep that in mind.
ok, the result of extern is that it will get ignored if put next to class:

...\outpost\outposti.h(42) : warning C4091: 'extern ' : ignored on left of 'class TOutpost_Input' when no variable is declared
A thought hits me. What if the fact that the .h files in my Outpost engine includes the .cpp files is the source of the problems? Because when I reinclude it in another .cpp file, it also reincludes the .cpp files, thereby redefining the functions. I think that might be the cause. How would I go around to fix this problem? Add #ifndef to .cpp files? :)

My goal is to make a DirectX engine that is independent of the game, so I can reuse it again easily. Thanks for your help.
I'm still stumped on this... Any ideas or suggestions? I've tried looking up tutorials, but apparently they didn't have tutorials on this. I'll continue research on this, if someone will be willing to help, I will really appreciate it!

This is an example error message:
WinMain.obj : error LNK2005: "public: __thiscall TParticle::TParticle(void)" (??0TParticle@@QAE@XZ) already defined in GameInit.obj

WinMain.cpp included the Outpost.h, and when I include it in GameInit also, I get those error messages. One for each function in the engine.
Do you have the inclusion guards in all of your header files? Also are you declaring variables used throughout multiple files with the extern command?

Could you post a copy of WinMain.cpp, Outpost.h and Outpost.cpp please?
i'm totally confused. What are you including you .cpp files for? While C++ has no formal rules on file extensions, the idea behind .h and .cpp is that the .h extension is used for things which are meant to be #included into other files (declarations of class, functions, and extern variables) and .cpp is meant to be used for things that are supposed to be compiled (implementations of functions and allocation of variables).

please post a post with a little tree showing what files are including what ... like this:

MyClass.h: nothing
MyClass.cpp: MyClass.h
AnotherClass.h: MyClass.h
AnotherClass.cpp: AnotherClass.h
Main.cpp: Myclass.h, AnotherClass.h

and then if you want you can tell us where the relevant parts are ...

btw, the #ifndef guards are for dealing with the situation where a header file is included in more than 1 HEADER file, not more than 1 cpp file. Look at my example above and you will see MyClass.h is included in MyClass.cpp, AnotherClass.h, and Main.cpp. The inclusions in MyClass.cpp and Main.cpp do not interfere with each other, if you headers only contain what the should ... but the inclusion in AnotherClass.h causes it to end up in Main.cpp twice ... which is not good .... hence the use of inclusion guards.

Each .cpp file is compiled COMPLETELY SEPERATELY from every other .cpp file ... hence the reason you have to #include <list> in EVERY .cpp file that uses a list, not just any 1 file in your project.

This topic is closed to new replies.

Advertisement