Advertisement

Header Files?

Started by January 29, 2000 12:38 AM
14 comments, last by Esap1 24 years, 10 months ago
All my other games ive been doing it wrong(little games), I put all the functions into a header file, not just predefine them, I have all the function code, and it worked, But How do I do it with a header file with predifend functions and then declared in a cpp file, I know Im stupid sorry
You''re not stupid, just learning...
Anyway, all the C/C++ compiler needs to let you call a function is the declaration of it (the function header) not the implementation (the body). Which means, all it needs is:

void Function1(int, float);

It doesn''t even need parameter names in the header. But, of course, the function implementation still needs to be linked in to the exe file. So, all you need to do is add an h file and a cpp file to your project. In the h file type:

void Function1(int, char*);

and in the cpp file type:

void Function1(int age, char* name)
{
// function code here
}

That should be all there is to it! Just make sure your cpp file is a part of the project, and gets linked in, or else you will get "undefined external" linker errors.

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
Advertisement
coo, but does the header need to be included, if its already in the project?
At the top of your header file add

 #ifndef _headerfilename_h_#define _headerfilename_h_// here is your function prototypes#endif  

That will prevent the header being included multiple times and causing errors. You must still use..
#include"headerfilename.h"  

... in every source file that uses it.

Edited by - Gromit on 1/29/00 3:58:06 PM
William Reiach - Human Extrodinaire

Marlene and Me


If you use vc 6, then it makes sense to put the names of the variables in the Declaration becuase it makes it easier to use with VC''s auto complete, though it is not necessary.
I wanted to know if you should define all your defines in one file, because several files need the same defines, also, several files need global variables, should I define all the global variables in one file?
Advertisement
Put the defines in whatever files you want, and whatever files they go best in. Put all enemy defines in enemy.h, and all level defines in level.h, etc. or whatever your file names are.
As far as globals go, you need to define them in a cpp file, and then in the h file that goes with that cpp file, declare it extern. Like this:

Enemies.cpp:
int numActiveEnemies = 0;

Enemies.h:
extern int numActiveEnemies;

Hope that helps.
Gottit it all compilin wit out errors, THANKS, the only problem is I F@#Ked it up some how in the proccess and my game freezes before it sets the video mode, maybe it was the new Voodoo2 drivers Im using, (but they work in Q3), whatever, Ill fix it somehow, anyway, thanks again!
I have a question: Why is it bad to put the entire function into the header file? I''m sure there must be a reason but i''m unclear as to what that reson is. I''m also aware the the function body does not need to be written before it is called. But I can''t see how putting it into a header file could hurt anything. So please enlighten me. Thanks
If you put the entire body of the function in the .h file, then the following happens:

- You include the .h file in more than one of your .cpp files.
- Each one of those .cpp files, when compiled, produces a .o (.obj) file, containing that function.
- When the linker tries to link all your .o files together, it finds multiple functions with the same name.

-Brian

This topic is closed to new replies.

Advertisement