Re: Pointer Issues

From: Gero Flucke <flucke_at_mail.desy.de>
Date: Wed, 22 Jun 2005 08:27:30 +0200 (CEST)


> Hi, I have the following, relatively simple piece of code that I am
> trying to run in Root, V4.03/02 on SL3.
>
> #include "TFile.h"
> #include "TString.h"
> #include "TH1.h"
> #include "iostream.h"
>
> TFile* f=new TFile("./DAT/slg_ecalc.root");
>
> void getHIST(TH1F* hist, int i) {
> TString emcobj;
> emcobj = "emc1"; emcobj+=i;
> hist = (TH1F*)f->Get(emcobj);
> //hist->Draw();
> }
>
> void get() {
>
> TH1F* hist;
> getHIST(hist, 2);
> cout << " hist = " << emechist << endl;
> hist->Draw();
>
> }

<snip>

>
> It seems as if the histogram is out of scope outside the function. The
> funny thing is if I change
> void getHIST(TH1F* hist, int i) to void getHIST(TH1F&* hist, int i)
> the macro works, and the histogram is drawn, but I cannot compile since
> it is illegal to have a pointer to a reference.

Hi Chris,
you should pass the pointer by reference instead of by value:

void getHIST(TH1F *&hist, int i)

Cheers

        Gero

PS: If your problem is really as simple, I'd prefer to use a return value:

TH1F* getHIST(int i) Received on Wed Jun 22 2005 - 08:27:37 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:09 MET