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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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