C++ problem - passing arguments

My main is similar to this:

void main()
{
...........
   TH1F *h;
   Int_t tlive, treal, ch_low, ch_hi;
   Char_t filename[] = "filename.Spe";
   get_data(filename, &tlive, &treal, &ch_low, &ch_hi, h);
   h->SetTitle("Title"); //  <== Error here! illegal pointer to class objet <--
...........
}

the function get_data get data from a file formatted in a special way and fill the histogram:

void get_data(Chat_t filename[], Int_t *tlive, Int_t *treal, Int_t *ch_low, Int_t *ch_hi, TH1F *histo)
{
   Int_t fch_lo, fch_hi, ftlive, ftreal;
..........
    histo = new TH1F(....5 parameters.....);
   
   for(......)
   {
      histo->Fill(......);
   }
   histo->Draw(); //this is only a test, but it work

   *tlive = ftlive;
   *treal = ftreal;
}

why doesn’t it work? I don’t understand.

Hi,

define your func as
void get_data(Char_t filename[], Int_t *tlive, Int_t *treal, Int_t *ch_low, Int_t *ch_hi, TH1F *&histo) {…
and it will. See your favorite C++ book on “passing by reference”.

Cheers, Axel.

thanks, thanks very much. Without you (and Rene) I will be in trouble. But I don’t understand: if I pass the pointer *histo the function can modify the object histo, or not? why it doesn’t work? You pass a reference to a pointer, but I think it is redundance.

Hi,

as you said - if you pass the pointer to the histogram, you can modify the histogram in get_data, and allow the calling function to see the modified histogram. But that’s not what you do - you modify the pointer to the histo (before it’s 0, after it’s !=0). So you have to pass the pointer to the histo as reference (or TH1**, but IMHO TH1*& is a lot nicer).

Cheers, Axel.

ok, thanks very much, now it’s all clear