A variable declaration at namespace scope is always a definition unless you put extern
on it; then it's just a declaration.
An important rule in C++ is that you can't have multiple definitions of objects with the same name. If your header file just contained double LIST1[100][100];
, this would work as long as you only ever included it in one translation unit. But as soon as you include the header file in multiple translation units, you have multiple definitions of LIST1
. You've broken the rule!
So to have a global variable accessible from multiple translation units, you need to make sure there is only a declaration in the header file. We do this with extern
:
extern double LIST1[100][100];
However, you cannot just include the header and try to use this object because there isn't a definition yet. This LIST1
declaration just says that an array of this type exists somewhere, but we actually need to define it to create the object. So in a single translation unit (one of your .cpp
files usually), you will need to put:
double LIST1[100][100];
Now, each of your .cpp
files can include the header file and only ever get the declaration. It's perfectly fine to have multiple declarations across your program. Only one of your .cpp
files will have this definition.