This is what i understand as your problem, I hope i am rite because haven't explained the problem well.
Problem-> you have created a header file which you hope to include in many files of the same program. It gives error that multiple definitions found.
Solution->
Seeing your code I can see that you haven't done any checking for previous definitions of the same header file.
So, what happens is , every where you include the header file "playerdatabase.h" the precompiler copy pasts the code from header file to your source file.
It works if your program is containing only one occurrence of "#include "playerdatabase.h" " macros. but in here you have it several times.
So, What happens is that your header file is copy pasted to the source file several times(one copy per #include "playerdatabase.h" ).
So, of course, when Pre-compiler gives the Pre-compiled source code to the actual C compiler it sees multiple occurrences of the same functions or variables or both.
What you have to do is make your pre-compiler check whether the header file is already copy pasted to the source code.
you can do it with a simple macro and always remember to do it for every header file you write in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <stdio.h>
#ifndef PLAYERDATABASE_H
#define PLAYERDATABASE_H
/*
...
header file content ( all your code )
...
*/
#endif
|
What happens is when precompiler see the ifndef directive and it means "If PLAYERDATABASE_H is not defined do what you want to do to the codes below until you see endif directive". when the precompiler meets this header file for the first time during a compilation, as it havent defined any Macro Name called PLAYERDATABASE_H, the if condition becomes true,
and it continues to the next line.
Then the precompiler sees define directive which defines the macro name PLAYERDATABASE_H and defines to it self that there exists a Macro Name called PLAYERDATABASE_H.
then it will continue and copy all your code to the sourcefile until it meets endif.
when the precompiler meets the header file again, when it checks for the Macro Name PLAYERDATABASE_H, it sees that it is defined, and if condition becomes false, which makes the precompiler go to the endif location , without copy pasting your header again.
if there is some code after the endif it will of course copy paste it or follow any directive you given there too. macros are awsome stuff.
ps.
in case you dont know about the precompiler,
it is a part of the compilers, code is first passed to it to process all the macros and stuff. dont worry every C compiler has a precompiler.
I hope this long reply will help you better.
but if i misunderstood your problem, i am very sorry..
thanks
cheers..