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 The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top