Hey guys I was wondering if you could help me out with this little problem I am having. I have this header file that has a set of 6 structure pointer that are used for loading images to the screen. I also had included a few functions into the header file. it worked perfectly fine when I had included the header file into 1 cpp file which was main.cpp. However when I wanted to use the header file to create a menu. I included the header file for menu.cpp and main.cpp. This ending up giving me errors.
like this,
1 2 3 4 5 6
main.obj : error LNK2005: "struct SDL_Surface * xplayer" (?xplayer@@3PAUSDL_Surface@@A) already defined in graphics.obj
main.obj : error LNK2005: "struct SDL_Surface * oplayer" (?oplayer@@3PAUSDL_Surface@@A) already defined in graphics.obj
1>main.obj : error LNK2005: "struct SDL_Surface * board" (?board@@3PAUSDL_Surface@@A) already defined in graphics.obj
1>main.obj : error LNK2005: "struct SDL_Surface * background" (?background@@3PAUSDL_Surface@@A) already defined in graphics.obj
1>main.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in graphics.obj
So I took out the function from the header and placed them in a .cpp and left a prototype but for the struct pointers. I need them to be able to be used in different .cpp files. Here is the file below.
Not all globals are bad. Some are good. For example, an "options" object that keeps track of all the program options and resources for you.
The trick is to know when to and when not to use globals, and if you need them, how to do it.
Again, a header file is not the place to plant code that will create a global variable.
However, a header file is the perfect place to put code that describes that global variable, so every source file that includes that header can use it.