0

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.

2
  • how is your class SDL_Rect defined? may be the class SDL_Rect doesn't allow objects to be created using the default constructor. Commented Jul 25, 2011 at 18:42
  • Has nothing to do with SDL_Rect, like I said, the same error occurs when I try to use ints, or anything else. Commented Jul 25, 2011 at 18:58

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

3 Comments

I guess I just don't understand why I have to do this. How is initializing the variables in a header file different than just putting global variables in main.cpp above the main function? Sorry I'm kind of a new programmer. I need these variables to be global across several different files; what is a better way to do that than putting them in a header file that all the other files can just #include?
@Slims "extern" keyword is your friend. Very, very slim shady example: in global.cpp declare int g_myGlobal = 111; then in main.cpp use it extern int g_myGlobal;
I did that and I got the exact same error message. Note that I have other non-array declarations and initializations of variables in my Global Variable header file and they all work fine without using the extern thing. It's only when I use an array where I start getting errors.
1

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++.

Comments

1

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,

1 Comment

I'm sorry, I'm kind of a noob. My prof last semester told us that we should use all caps when declaring global variables. Is this wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.