I'm not very used and don't really understand the use of global variables in c++, and cannot manage to solve a problem here, although I'm nearly sure I'm using the declarations the right way. I've got an unordered map, which contains params from a file. This is working fine, I've outputted the contents, everything ok. But I would like this map to be available everywhere in my program, so that I don't have to pass it as an argument every time, hence the use of global variable. I've a little typedef :
typedef tr1::unordered_map<string,float> unmap;
then the function to read the parameters:
unmap readParameters(string filename);
then the global scope declaration (everything outside the main of course, although in the same file, I still don't have any headers):
extern unmap params;
The affectation is made in the main() function:
unmap params=readParameters(inputfile_name);
(Note: I tried with and without the "unmap", but same error). And I've got a function displayInformation() to display its contents in which I use my map "params":
for (unmap::iterator it=params.begin();it!=params.end();it++){
cout<<it->first<<" = "<<it->second<<endl;
}
But g++ moans then :
Undefined symbols for architecture x86_64:
"_params", referenced from:
_main in loca.o
displayInformation() in loca.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [loca] Error 1
So how should I declare/use this to be able to see it from any function?