redefining

Aug 31, 2013 at 3:17pm
Hello,

I'm trying to get my .cpp files to interact with eachother using .h files. But everytime I try to compile it it says the variables are already defined in the header file.

I get these errors:
1
2
3
4
5
6
c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.cpp(37): error C2086: 'SDL_Surface *horStripeImage' : redefinition
1>          c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.h(6) : see declaration of 'horStripeImage'
1>c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.cpp(38): error C2086: 'SDL_Surface *verStripeImage' : redefinition
1>          c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.h(7) : see declaration of 'verStripeImage'
1>c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.cpp(39): error C2086: 'SDL_Surface *playfieldImage' : redefinition
1>          c:\users\gebruiker\desktop\grid control\grid control\grid control\loadimages.h(8) : see declaration of 'playfieldImage'


This is what my files look like:

loadImages.h
1
2
3
4
5
6
7
8
9
10
#ifndef LOADIMAGES_H_GUARD
#define LOADIMAGES_H_GUARD

#include "SDL.h"

SDL_Surface* horStripeImage;
SDL_Surface* verStripeImage;
SDL_Surface* playfieldImage;

#endif 


loadImages.cpp
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
32
33
34
35
36
37
38
39
#include "loadImages.h"

//Load stripe_horizontal.bmp
SDL_Surface* loadHorStripe()
{
	SDL_Surface* loadedImage = SDL_LoadBMP( "stripe_horizontal.bmp" );
	SDL_Surface* stripe_horizontal = SDL_DisplayFormat( loadedImage );

	SDL_FreeSurface( loadedImage );

	return stripe_horizontal;
}

//Load stripe_vertical.bmp
SDL_Surface* loadVerStripe()
{
	SDL_Surface* loadedImage = SDL_LoadBMP( "stripe_vertical.bmp" );
	SDL_Surface* stripe_vertical = SDL_DisplayFormat( loadedImage );

	SDL_FreeSurface( loadedImage );

	return stripe_vertical;
}

//Load playfield.bmp
SDL_Surface* loadPlayfield()
{
	SDL_Surface* loadedImage = SDL_LoadBMP( "playfield.bmp" );
	SDL_Surface* playfield = SDL_DisplayFormat( loadedImage );

	SDL_FreeSurface( loadedImage );

	return playfield;
}

//Assign variables
SDL_Surface* horStripeImage = loadHorStripe();
SDL_Surface* verStripeImage = loadVerStripe();
SDL_Surface* playfieldImage = loadPlayfield();


I think it has something to do with this
1
2
3
4
//Assign variables
SDL_Surface* horStripeImage = loadHorStripe();
SDL_Surface* verStripeImage = loadVerStripe();
SDL_Surface* playfieldImage = loadPlayfield();

but I don't know what to do. Please help me.

Thanks in advance.
Aug 31, 2013 at 3:41pm
In the header file you should declare variables, not define them. (Hence the redefinition error.)

You declare by using the extern keyword.

1
2
3
4
5
6
7
8
9
10
#ifndef LOADIMAGES_H_GUARD
#define LOADIMAGES_H_GUARD

#include "SDL.h"

extern SDL_Surface* horStripeImage;
extern SDL_Surface* verStripeImage;
extern SDL_Surface* playfieldImage;

#endif  

Aug 31, 2013 at 4:13pm
You can declare a variable/function many times, but there has to be only one definition. You have re-defined the variables in loadImages.cpp. Although i would use extern just as Catfish proposed, it's considered redundant in C++.
Aug 31, 2013 at 4:28pm
Although i would use extern just as Catfish proposed, it's considered redundant in C++.

How so? If you want to access a global variable defined in a source file, you will need to declare it as extern somewhere - best bet is the header file of that source file.
Aug 31, 2013 at 4:32pm
Although i would use extern just as Catfish proposed, it's considered redundant in C++.


I'll bite. For what reason is it considered redundant in C++?
Aug 31, 2013 at 4:40pm
@cire
I'll bite. For what reason is it considered redundant in C++?


For the same error as in the original post.:)
Aug 31, 2013 at 4:52pm
@cire, this is linked to the OP's example (again), but the following

1
2
3
 
extern int x = 3; 
int x = 3; 


are essentially equivalent, so had Staygold defined the images the first way, he could have just dropped the extern. Do note that it is not my personal opinion, but rather what i've read in "The C++ Programming Language" not too long ago. Apart from the
cases in which you have to use extern as Catfish mentioned, it also conveys your intents better and makes them explicit.


Last edited on Aug 31, 2013 at 4:58pm
Aug 31, 2013 at 5:03pm
@cire, this is linked to the OP's example (again), but the following

1
2
extern int x = 3; 
int x = 3; 


re essentially equivalent, so had Staygold defined the images the first way, he could have just dropped the extern.


You responded to and referenced Catfish's post, where extern was required and far from redundant. If the OP had moved the initialization and definition of the variables to the header file as you seem to be suggesting, he would've had redefinitions of the variables in multiple source files. I don't see your point.
Aug 31, 2013 at 5:07pm
According to the C++ Standard

2 A declaration is a definition unless ... it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function body,
Last edited on Aug 31, 2013 at 5:07pm
Aug 31, 2013 at 5:35pm
Extern solved the problem. Thanks alot.
Topic archived. No new replies allowed.