简体   繁体   English

头文件中的全局变量导致重定义错误c ++

[英]Global Variables in Header file causing redefinition error c++

I have a header file which holds all my global variables(and a cpp file to declare them) and I use the #ifndef #define #endif tags, but I still get redefinition errors 我有一个头文件,它包含我所有的全局变量(和一个cpp文件来声明它们),我使用#ifndef #define #endif标签,但我仍然得到重定义错误

I have a total of 3 header files and 4 cpp files, and all of the header / main.cpp contains the globalvar.h header file. 我总共有3个头文件和4个cpp文件,并且所有header / main.cpp都包含globalvar.h头文件。

Here is the code: 这是代码:

GlobalVar.h 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 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;

You have two options for how to deal with the constants which cause the trouble. 对于如何处理导致问题的常量,您有两种选择。

Option 1 选项1

Remove the extern from the header: 从标题中删除extern

#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 must not define the variables in GlobalVar.cpp . 如果这样做,则不得在GlobalVar.cpp定义变量。

Option 2 选项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 do need to define and initialize the constants in GlobalVar.cpp . 现在,您需要在GlobalVar.cpp定义和初始化常量。

This disadvantage of this is that you cannot use names such as SCREEN_WIDTH in contexts that require a compile-time integer constant, such as the dimensions of an array or the case clauses of a switch statement. 这样做的缺点是您不能在需要编译时整数常量的上下文中使用SCREEN_WIDTH等名称,例如数组的维度或switch语句的case子句。

So, option 1 is the technique which is used more often. 因此,选项1是更常用的技术。

You should only give the constants values in one place. 您应该只在一个地方给出常量值。

You an either keep the extern declarations in the header (without the values) and have the values in the cpp file, or remove the extern keyword and define the values in the header only. 您要么在标题中保留extern声明(没有值)并在cpp文件中包含值, 要么删除extern关键字并仅在标题中定义值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM