Creating TObjArray of histograms

The following code does not work: It gives an error —> [color=red]error: request for member ‘Add’ in ‘Hlist’, which is of non-class type ‘TObjArray*’
[/color]

How can I fix this?

[color=darkblue]…bunch of includes…

using namespace std;
char name[10], title[20];
int main(int argc, char **argv)
{

TObjArray Hlist(0); // create an array of Histograms
TH1F
h; // create a pointer to a histogram
// make and fill 15 histograms and add them to the object array
for (Int_t i = 0; i < 15; i++) {
sprintf(name,“h%d”,i);
sprintf(title,“histo nr:%d”,i);
h = new TH1F(name,title,100,-4,4);
Hlist.Add(h);
h->FillRandom(“gaus”,1000);
}
// open a file and write the array to the file
TFile f(“out.root”,“recreate”);
Hlist->Write();
f.Close();
return 0;
}[/color]

Hi,

Please choose if you want to create the list on the heap or on the stack (using pointer or reference):
Heap: TObjArray *Hlist = new TObjArray(0); [...] Hlist->Add(h); [...] Hlist->Write();
Stack: TObjArray Hlist(0); [...] Hlist.Add(h); [...] Hlist.Write();
But don’t mix both… :wink:

Cheers,
Bertrand.

That fixed it! Thank you very much!!

dube