Global Variable Madness

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
I'm currently using MSVC++ 2008 edition and i have a whole bunch of cpp and header files so i decided to make one for my global variables.
Unfortunately, I keep getting a "redefinition error" even though I only declared the variables once.
here is the header file
Code:
#ifndef GLOBALVAR_H
#define	GLOBALVar_H
#include "SDL.h"

extern const int SCREEN_WIDTH = 960;
extern const int SCREEN_HEIGHT = 960;
extern const int SCREEN_BPP = 32;

extern const int FRAMES_PER_SECOND = 30;
//tiles attribute
extern const int TILE_WIDTH = 64;
extern const int TILE_HEIGHT = 64;
extern const int TOTAL_TILES = 150;
extern const int TOTAL_SPRITES = 64;
//tile sprites
extern SDL_Rect clip[144];
//Images / backgrounds
extern SDL_Surface* screen;
extern SDL_Surface* background;
extern SDL_Surface* Ike;
extern SDL_Surface* thetiles;
#endif

here is the cpp file
Code:
#include "GlobalVar.h"

const int SCREEN_WIDTH = 960;
const int SCREEN_HEIGHT = 960;
const int SCREEN_BPP = 32;

const int FRAMES_PER_SECOND = 30;
//tiles attribute
const int TILE_WIDTH = 64;
const int TILE_HEIGHT = 64;
const int TOTAL_TILES = 150;
const int TOTAL_SPRITES = 64;
//tile sprites
SDL_Rect clip[144];
//Images / backgrounds
SDL_Surface* screen;
SDL_Surface* background;
SDL_Surface* Ike;
SDL_Surface* thetiles;

and this is the main file
Code:
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <fstream>
#include "GlobalVar.h"
#include "Tiles.h"
#include "Timer.h"
#include "Unit.h"




SDL_Surface* load_image(std::string filename)
{
	SDL_Surface* loadedImage;
	SDL_Surface* optimizedImage;

	loadedImage = IMG_Load(filename.c_str());
	
	if(loadedImage != NULL)
	{
		
		optimizedImage = SDL_DisplayFormat(loadedImage);
		if(optimizedImage != NULL)
		{
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF,0xFF));
		}
		return optimizedImage;
	}
}

void applysurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect *clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, destination, &offset);
}

bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return false;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

	if(screen == NULL)
	{
		return false;
	}

	SDL_WM_SetCaption("Fire Emblem",NULL);
	return true;
}

bool load_files()
{
	thetiles = load_image("Tiles.png");

	if(thetiles == NULL)
	{
		return false;
	}


	return true;
}

bool set_tiles(Tiles *tiles[])
{
	int x = 0;
	int y = 0;

	std::ifstream map("Tile.map");
	
	int TileType = -1;

	if(map == NULL)
	{
		return false;
	}
	for(int i = 0; i <TOTAL_TILES; i++)
	{
		map>>TileType;

		if(map.fail()== true)
		{
			map.close();
			return false;
		}
		if((TileType >-1) &&(TileType <64))
		{
			tiles[i] = new Tiles(int x, int y, TileType);

			x+=TILE_WIDTH;
			if(x > SCREEN_WIDTH)
			{
				x = 0;
				y+= TILE_HEIGHT;
			}
		}
	}
	map.close();
	return true;
}


int main(int argc, char* args[])
{
	bool quit = false;

	if(init() == false)
	{
		return false;
	}

	if(load_files() == false)
	{
		return false;
	}

	Timer fps;


	while(quit == false)
	{

I didn't finish the main file since I feel like i need to work more on other parts before i can make runable code. But I tested it to see if there are any simple errors i should fix before they become massive. However I can't fix the redefinition and see the declaration errors

:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(5) : error C2374: 'SCREEN_WIDTH' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(5) : see declaration of 'SCREEN_WIDTH'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(6) : error C2374: 'SCREEN_HEIGHT' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(6) : see declaration of 'SCREEN_HEIGHT'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(7) : error C2374: 'SCREEN_BPP' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(7) : see declaration of 'SCREEN_BPP'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(9) : error C2374: 'FRAMES_PER_SECOND' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(9) : see declaration of 'FRAMES_PER_SECOND'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(11) : error C2374: 'TILE_WIDTH' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(11) : see declaration of 'TILE_WIDTH'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(12) : error C2374: 'TILE_HEIGHT' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(12) : see declaration of 'TILE_HEIGHT'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(13) : error C2374: 'TOTAL_TILES' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(13) : see declaration of 'TOTAL_TILES'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(14) : error C2374: 'TOTAL_SPRITES' : redefinition; multiple initialization
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(14) : see declaration of 'TOTAL_SPRITES'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\fire emblem engine.cpp(97) : error C2062: type 'int' unexpected
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\fire emblem engine.cpp(131) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\garry\desktop\visual studios\fire emblem\fire emblem\fire emblem engine.cpp(130)' was matched
1>Tiles.cpp
1>Unit.cpp
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\unit.cpp(3) : error C2039: 'Movement' : is not a member of 'Unit'
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\unit.h(6) : see declaration of 'Unit'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\unit.cpp(5) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\garry\desktop\visual studios\fire emblem\fire emblem\unit.cpp(4)' was matched
1>GlobalVar.cpp
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(3) : error C2370: 'SCREEN_WIDTH' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(5) : see declaration of 'SCREEN_WIDTH'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(4) : error C2370: 'SCREEN_HEIGHT' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(6) : see declaration of 'SCREEN_HEIGHT'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(5) : error C2370: 'SCREEN_BPP' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(7) : see declaration of 'SCREEN_BPP'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(7) : error C2370: 'FRAMES_PER_SECOND' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(9) : see declaration of 'FRAMES_PER_SECOND'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(9) : error C2370: 'TILE_WIDTH' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(11) : see declaration of 'TILE_WIDTH'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(10) : error C2370: 'TILE_HEIGHT' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(12) : see declaration of 'TILE_HEIGHT'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(11) : error C2370: 'TOTAL_TILES' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(13) : see declaration of 'TOTAL_TILES'
1>c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.cpp(12) : error C2370: 'TOTAL_SPRITES' : redefinition; different storage class
1> c:\users\garry\desktop\visual studios\fire emblem\fire emblem\globalvar.h(14) : see declaration of 'TOTAL_SPRITES'
1>Generating Code...

Any tips =)
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
I imagine that ifndef does case sensitive lookup of defined names.
GLOBALVAR_H != GLOBALVar_H

ahhhhhhhhhhhhhh no wonder. Thanks!!!!!!
When i fixed it, and didn't include the header file with the globals anywhere. I got the error message of redefinition storage class. But when i included it in the main.cpp, the error messages disappeared. Does that mean it's fixed? =)
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Instead of defining a ton of extern variables and getting tons of redifinition errors (which happens quite a lot ;3) you can encapsulate everything:

Code:
struct GlobalData{
     static const int SCREEN_WIDTH = 960;
     static const int SCREEN_HEIGHT = 960;
     static const int SCREEN_BPP = 32; 

     SDL_Surface* screen;
     SDL_Surface* background;
     SDL_Surface* Ike;
     SDL_Surface* thetiles;
}

static GlobalData& GetGlobalData(){
    static GlobalData s_gl;
    return s_gl;
}

Code:
int main(){
     GetGlobalData().screen->DoStuff();

    return 0;
}

If you avoid globals then you avoid redefinitions mostly.
And you can conveniently use it to initialize your globals since static locals call constructors just like normal:

Code:
GlobalData(){
    thetiles = new SDL_Surface[20];
}

~GlobalData(){
    delete[] thetiles;
}
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
the problem actually was that I was defining the variable, e.g. extern const int SCREEN_WIDTH = 640; When i should just be stating that they exist
Thus i had extern const SW = 640 in the header and const SW = 640 in the cpp file
I fixed it by removing = 640 in the header file and just having extern const int SCREEN_WIDTH;
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top