If I declare a global variable in, say, "main.cpp", then try to access it in, say, "file2.cpp", it won't work (thats right, right?). Earlier on, I read in a book that you can declare all of your global variables in a header, then include it in another file, then declare it using "extern *variablename*" in the second file... then, it said something like you can include the header in yet another file and use extern again, and then both files will have access to the globals. I tried it, but then it gave me an error:

Missing ';' before identifier 'Ship' //this was in globals.h

That usually means that 'Ship''s header hasn't been included yet... but it WAS. So then I tried not including the header with the globals (as in, I included it in 'main.cpp' but not in the second file), and then deleted all the stuff in the second file that depended on these global variables, and then it worked. Does anybody know why? :confused: (Or, for that matter, how I can share the global variables between files?)

07-19-2002

Salem

This goes in a header file, which is included in all the source files which access foo
extern int foo;

This goes in ONE source file to declare the variable
int foo;

07-20-2002

Hunter2

so...

Code:

//global.h

int foo;

Code:

//main.cpp

#include "global.h"

extern int foo;

Code:

//file2.cpp

#include "global.h"

extern int foo;

right?... I tried that, but then it gave me an error in global.h...

07-20-2002

cozman

show more of your global.h file.. what you showed is perfectly fine.. what is the current error + show the lines around the line it reports the error on.