Headers included in different .cpp

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#ifndef GRAPHICS_H
#define GRAPHICS_H

// The headers
#include "SDL.h"
#include "SDL_image.h"
#include "string"

// the attributes to the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// the SDL surfaces used
SDL_Surface *xplayer = NULL;
SDL_Surface *oplayer = NULL;
SDL_Surface *board = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

bool init ();

SDL_Surface *load_image ( std::string filename );

void apply_surface ( int x, int y, SDL_Surface *source, SDL_Surface *destination );

void cleanup ();

#endif


Thanks for the help and sorry for any typos.
Last edited on
Try deleting the obj files located in your project.
@Lumpkin -- not the problem

@Stormhawk
Uh, is this a question, or are you trying to explain something for other people?
@duoas
It is a question, I'm just trying to explain my problem so you can understand it.
Well if that's not the problem, where are the rest of the source files?
Globals are bad.

Globals in headers are worse.

I explain the problem you're seeing in this post:

http://www.cplusplus.com/forum/general/71964/#msg383802

Though the real solution is "don't use globals".
A header file is just to explain what exists in other source files. Whereas source files get compiled, header files do not.

Here's the quick and easy way to understand headers and source modules:
http://www.cplusplus.com/forum/general/13162/#msg63354

Once you read that, try to separate your stuff appropriately. Good luck!
@ Disch

Funny thing is I never used to use globals I just saw them a lot of more complicated pieces of code so I thought why not try it out a bit.

I also saw a lot definitions in headers which I never used to do either. So I am just trying different ways.

I'll read both of your links, thanks

edit:

Also I notice extern don't seem to work on struct pointers.. or maybe I am doing it wrong.
Last edited on
Stormhawk wrote:
Funny thing is I never used to use globals I just saw them a lot of more complicated pieces of code so I thought why not try it out a bit.
Funny thing is, the code is generally complicated because it has to deal with the irks and pains of using global variables ;)
Last edited on
@ LB

lol I can't lie I was raging earlier when I had to deal with trying to get it to work.
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.

That's essentially the point.

Headers describe global things:
- functions
- types
- variables
- constants
- etc

Source files (which get compiled into actual binary code) are where the things described by headers actually live.
Last edited on
Topic archived. No new replies allowed.