问题在全局变量头文件C ++中初始化数组 [英] Problem Initializing an Array in a Global Variables Header File C++

查看:240
本文介绍了问题在全局变量头文件C ++中初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SDL编写一个简单的程序,但我遇到了一个奇怪的问题。首先是代码:

  #ifndef GLOBAL_VARIABLES_H 
#define GLOBAL_VARIABLES_H

#include<串GT;
#include< cassert>
#includeSDL.h
#includeSDL_image.h
using std :: string;

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

const string MAIN_BACKGROUND_FILENAME =tempBackground.jpg;



SDL_Rect CARD_CLIPS [2];

const int CARD_BACK = 0;
const int CARD_FRONT = 1;

CARD_CLIPS [CARD_BACK] .h = 196;
CARD_CLIPS [CARD_BACK] .w = 286/2;
CARD_CLIPS [CARD_BACK] .x = 286/2;
CARD_CLIPS [CARD_BACK] .y = 0;

CARD_CLIPS [CARD_FRONT] .h = 196;
CARD_CLIPS [CARD_FRONT] .w = 286/2;
CARD_CLIPS [CARD_FRONT] .x = 0;
CARD_CLIPS [CARD_FRONT] .y = 0;

#endif

我收到的错误是这样的: p>

  1> c:\ users \  -  \ global variables.h(23):error C2466:can not allocate a array 0 

1> c:\ users \ - \ global variables.h(23):error C2143:syntax error:missing';'before'。'

1> c:\ users \ - \ global variables.h(23):错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int

1> c:\users\ - \ global variables.h(23):error C2371:'CARD_CLIPS':redefinition;不同的基本类型

1> c:\ users \ - \ global variables.h(18):参见'CARD_CLIPS'的声明

重复每次尝试初始化SDL_Rect元素时的相同错误。



请注意, SDL部分与问题无关。如果我尝试声明一个int数组并以相同的方式初始化它,我会得到完全相同的错误。如果我把它放在我的main.cpp中,它就可以工作得很好。



感谢您的帮助。编辑:请注意,除了当我尝试在头文件中使用数组时,我不会收到任何错误。虽然我做了什么来理解传统的做事方式,但我也想理解为什么从基本的角度来看,我不能像这样在头文件中声明和初始化数组。

extern 声明。



其次,您可以初始化定义中的变量(包括数组),或将内容指定为函数内的可执行语句。您不能将可执行语句放在文件范围内。






数组初始化如下所示:

  int a [4] = {1,4,9,16}; 

不是这样:

  int a [4]; 
a [0] = 1; //非功能之外的非法!
a [1] = 4;
a [2] = 9;
a [3] = 16;


I'm writing a simple program using SDL, but I'm running into a strange problem. First the code:

#ifndef GLOBAL_VARIABLES_H
#define GLOBAL_VARIABLES_H

#include <string>
#include <cassert>
#include "SDL.h"
#include "SDL_image.h"
using std::string;

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

const string MAIN_BACKGROUND_FILENAME = "tempBackground.jpg";



SDL_Rect CARD_CLIPS[2];

const int CARD_BACK = 0;
const int CARD_FRONT = 1;

CARD_CLIPS[CARD_BACK].h = 196;
CARD_CLIPS[CARD_BACK].w = 286/2;
CARD_CLIPS[CARD_BACK].x = 286/2;
CARD_CLIPS[CARD_BACK].y = 0;

CARD_CLIPS[CARD_FRONT].h = 196;
CARD_CLIPS[CARD_FRONT].w = 286/2;
CARD_CLIPS[CARD_FRONT].x = 0;
CARD_CLIPS[CARD_FRONT].y = 0;

#endif

The error I'm getting is this:

1>c:\users\--\global variables.h(23): error C2466: cannot allocate an array of constant size 0

1>c:\users\--\global variables.h(23): error C2143: syntax error : missing ';' before '.'

1>c:\users\--\global variables.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\--\global variables.h(23): error C2371: 'CARD_CLIPS' : redefinition; different basic types

1>c:\users\--\global variables.h(18) : see declaration of 'CARD_CLIPS'

Repeat same error for each time I try to initialize an element of the SDL_Rect.

Note that the SDL part has nothing to do with the problem. If I try to declare an int array and initialize it in the same way, I get the exact same error. If I put any of this in my main.cpp it works completely fine.

Thanks for the help. Let me know if additional information is needed.

EDIT: Note that I get no errors except for when I try and use arrays in the header file. Though I do what to understand the conventional way to do things, I also want to understand why, from a fundamental standpoint, I can't declare and initialize arrays in a header file like this.

解决方案

First, variable definitions shouldn't be in header files, only extern declarations.

Second, you can initialize variables (including arrays) in the definition, or assign the content as executable statements inside a function. You can't put executable statements at file scope.


Array initialization looks like this:

int a[4] = { 1, 4, 9, 16 };

not like this:

int a[4];
a[0] = 1;  // ILLEGAL outside a function!
a[1] = 4;
a[2] = 9;
a[3] = 16;

这篇关于问题在全局变量头文件C ++中初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆