Re: STL Map Scoping Issue
Hi!
sfncook@gmail.com schrieb:
Wow! You guys are great! Thanks for the responses so far. However, I
forgot to mention one factor that must make the difference: The code
is broken up into different files.
Indeed that is the difference, as Gianni already pointed out. :)
********* typedefs.h *****************
#include "stdlib.h"
#include <map>
#include <vector>
typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;
static std::map<int, ShipType *> shipTypeMap;
This definition of shipTypeMap includes "static". That makes shipTypeMap
appear mutiple times: once for each translation unit (= cpp file). So
your initialisation code initialises one instance while the other code
accesses the uninitialised one => oops. You need to do the following:
typedefs.h:
extern std::map<int, ShipType*> shipTypeMap;
somewhere.cpp:
std::map<int, ShipType*> shipTypeMap;
This makes one global instance of shipTypeMap which is then access from
all code.
HTH,
Frank