Compile error with global variable.

Hello, I'm trying to declare a global map like this:

1
2
3
4
5
6
7
8
9
10
// These work as they should
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *update_timer = NULL;
const short DISPLAY_WIDTH = 512;
const short DISPLAY_HEIGHT = 800;
const short FPS = 60;

// This one goes wrong
std::map<ALLEGRO_EVENT_TYPE,vector<GameEntity*>>* registered_events = NULL;


This produces the compiler error:

expected constructor, destructor, or type conversion before ‘<’ token

How do I declare a global variable?
You can't define values in the global scope. The global scope doesn't run sequentially.

Remove the "= NULL" part and it's gonna work. And after the vector<GameEntity*> put a space, so it has to be <GameEntity*> >, and not >>.
I changed it like this, but it gives me the same error.

std::map<ALLEGRO_EVENT_TYPE,vector<GameEntity*> > registered_events;

Nor does it compile when I remove the "vector<>" part, same error:

std::map<ALLEGRO_EVENT_TYPE, GameEntity*> registered_events;

Also, about the "defining values" part, why do the statements above the wrong declaration work correctly?
Last edited on
closed account (zb0S216C)
Werner2 wrote:
1
2
std::map<ALLEGRO_EVENT_TYPE,vector<GameEntity*>>* registered_events = NULL;
//                                            ^^ Here 

If you're using C++03, place a space between the highlighted > symbols. The compiler thinks it's a right-shift operation. It supposed to look like: ...<ALLEGRO_EVENT_TYPE, vector<GameEntity*> > ...

Wazzak
Did you try using "std::vector" instead of "vector"?
Or ALLEGRO_EVENT_TYPE* ?
Anyways:
std::map<ALLEGRO_EVENT_TYPE,std::vector<GameEntity*>>* registered_events = NULL;
This will work anyways, because it's a POINTER, not a variable.
You can define values in the global scope anyways.
I used to do things like this in the global scope:
1
2
char * Array = new char[Size];
bool ClearArray = (memset(Array,0,Size * sizeof(char)) != 0);

And i never got a problem doing that. Maybe it's not the best way to do things, but sure it will be done before everything else from the EntryPoint.
Last edited on
first thing it has to be std::vector, since you're not using the

using namespace std;

This line compiled with me with no problems, after including vector and map in the program:


std::map<double,std::vector<int> > registered_events;

Are you sure your types are defined properly?
@EssGeEich this is not standard C++ (about assigning stuff in global scope). Perhaps you can do that with simple types, but a constructor of such a complicated object like map won't run in the global scope.
It's a std::map pointer, check it twice. And, as it is a pointer, you can assign NULL to it to make sure you know it's a invalid pointer. What i mean is, In the "Debug" mode (At least under MSVS) every variable's default value isn't 0 (NULL is 0). So if you don't assign 0 to it, it will be something like 0xcdcdcdcd or 0xfefefefe - and how would you check if a pointer is valid or not? The fastest way is to check if it's a 0 (0x0), but what if you get a 0xcdcdcdcd-like value? Is it valid or not? P.S.: Don't think about this post as a disrespectful reply.
Last edited on
I changed it to look like this:

std::map<int,int> registered_events;

It still gives me the same error.

Also, the compiler doesn't seem to care wether it's a pointer or not, it still gives me the same error:

std::map<int,int> * registered_events;

or

std::map<int,int> * registered_events = NULL; (still the same error.)

I'm using the GCC compiler that came with my Debian Squeese OS.
Last edited on
You can initialise your pointer in your main, it doesn't make a difference, because it's still not standard C++ to initialise stuff outside a sequential function.

And actually, that's why there's something called static variables, through which you can use a single variable along the whole code (per se, global), and it's always initialised by the constructor of the object you're creating (without having to do it explicitly).

static variables is the legal solution to globalising objects with them being automatically initialised.
Werner2, you definitely haven't included map in your program. Make sure the line

#include <map>

is at the top of your program.
Did you try including the map header before declaring the variable? Like this:
1
2
#include <map>
std::map<...
@TheDestroyer and EssGeEich:

The compile error is gone. Thanks a lot. (Why can I overlook such a simple thing?)
Last edited on
Happens :)

Please mark the question as solved.
Topic archived. No new replies allowed.