Problem Initializing an Array in a Global Variables Header File C++
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 assume开发者_Go百科d. 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;
This article might provide some guidelines on what should be included in a header file. It says C in the title but of course it's applicable to C++.
As Ben Voigt has already explained, your problem has nothing to do with header files. It is simply that you have put ordinary executable statements that are not declarations, directly at namespace scope (that is, outside any function or class). You can't.
This statement is technically fine:
const int CARD_BACK = 0;
It's technically fine because it is a declaration. There is no assignment. The =
here does not denote assignment, but is part of the declaration syntax, indicating that an initializer follows.
I say "technically" because you really should reserve ALL UPPERCASE names for macros, but the compiler doesn't care.
On the other hand, this statement is not OK at namespace scope:
CARD_CLIPS[CARD_BACK].h = 196;
That's because it's not a declaration, it is not introducing a new name: it's an assignment.
Oh, my eyes hurt from all that uppercase!
Cheers,
精彩评论