Global variables in header file causing C ++ override error

I have a header file that contains all my global variables (and a cpp file to declare them) and I use the #ifndef #define #endif tags, but I still get override errors

I have a total of 3 header files and 4 cpp files, and the entire /main.cpp header contains the globalvar.h header file.

Here is the code:

GlobalVar.h

 #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; extern SDL_Event event; #endif 

GlobalVar.cpp

 #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; SDL_Event event; 
+6
source share
2 answers

You have two options for dealing with constants that cause problems.

Option 1

Remove extern from header:

 #ifndef GLOBALVAR_H #define GLOBALVAR_H #include "SDL.h" const int SCREEN_WIDTH = 960; const int SCREEN_HEIGHT = 960; const int SCREEN_BPP = 32; const int FRAMES_PER_SECOND = 30; const int TILE_WIDTH = 64; const int TILE_HEIGHT = 64; const int TOTAL_TILES = 150; const int TOTAL_SPRITES = 64; extern SDL_Rect clip[144]; extern SDL_Surface* screen; extern SDL_Surface* background; extern SDL_Surface* Ike; extern SDL_Surface* thetiles; extern SDL_Event event; #endif 

If you do this, you should not define variables in GlobalVar.cpp .

Option 2

Remove the initializers from the header:

 #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; extern const int TILE_WIDTH; // = 64; extern const int TILE_HEIGHT; // = 64; extern const int TOTAL_TILES; // = 150; extern const int TOTAL_SPRITES; // = 64; extern SDL_Rect clip[144]; extern SDL_Surface* screen; extern SDL_Surface* background; extern SDL_Surface* Ike; extern SDL_Surface* thetiles; extern SDL_Event event; #endif 

Now you need to define and initialize the constants in GlobalVar.cpp .

This drawback is that you cannot use names such as SCREEN_WIDTH in contexts that require an integer constant, such as the size of an array or the case clause of a switch .

So option 1 is the method that is used more often.

+7
source

You must specify constant values ​​in only one place.

You either save the extern declarations in the header (no values) and have the values ​​in the cpp file, or delete the extern keyword and define the values ​​only in the header.

+2
source

Source: https://habr.com/ru/post/898922/


All Articles