Reading a map

Hi, I’m attempting to read a map from a TTree using compiled root code. The relevant lines are:

main() {
gROOT->ProcessLine('#include<map>"); //this was needed to get it to recognize the map, although I include the same line in the file includes before main()
map<string,int> * map = 0;

//lines to get tree

t->SetBranchAddress("HLTNameMap", &map)

int evts = t->GetEntries();

for(int i = 0; i < evts; i++) {
     t->GetEntry(i);
     map<string,int>::const_iterator iter;
     for(iter = map->begin(); iter != map->end(); iter++) {
          cout<<iter->first<<endl;
     }

}

This does not compile. The error is:

test.C:55: error: expected primary-expression before ‘,’ token
test.C:55: error: expected primary-expression before ‘int’
test.C:55: error: expected `;' before ‘int’
test.C:57: error: ‘iter’ was not declared in this scope

Where 55 is the line number for the iterator declaration.

I can get this to compile and read out the tree if I add the line:

typedef map<string,int> MapType;

and use

MapType::const_iterator iter;

to declare my iterator. Why does the latter work, but not the former.

Hi,

Do not reuse the template name ‘map’ as a variable name, the compiler can not (really) know when you want to use one or the other …

Philippe.