I am trying to get objects to reference a few globalized variables (before you start hitting me for using globals putting them into an object would either make it unreachable by other objects, or require adding excess arguments to method calls) for example
main.cpp
bool firstLoop;
const int dt = 10;
thing.cpp
void thing::Update(Object * thingToUpdate){
if( firstLoop){
...
} else{
// working with dt
}
}
but every time I try to access the variables I get a error of “undeclared identifier”
do I need to mark them as extern, or something else.
In
thing.cpp, writeNote that the same trick will not work for
dtbecause it has internal linkage(because it’s a const variable of built-in type).Usually, you should have one header file where you declare(with
extern) your variables that need access from everywhere, and another source file which defines these variables. The constant variables of built-in type should be defined in the header file, or their linkage should be explicitly made external by means of the selfsame extern keyword. Then the header file should be included everywhere where access is needed. Example: