Passing data members between classes.

I'm having a problem passing data members between classes.


Double_t fiber_width = .1;

struct hit {
hit() {}
hit(Bool_t isTop_i, Int_t x_fiber_i, Int_t y_fiber_i, Double_t x_time_i, Double_t y_time_i) :
isTop(isTop_i),
x_fiber(x_fiber_i),
y_fiber(y_fiber_i),
x_time(x_time_i),
y_time(y_time_i)
{
if ( isTop ) {x = (x_fiber + 1)*fiber_width; y = (y_fiber - 63)*fiber_width;}
else {x = (x_fiber - 31)*fiber_width; y = (y_fiber - 95)*fiber_width;}
delay = y_time - x_time;
}
~hit(){}

Bool_t isTop;
Int_t x_fiber; Int_t y_fiber;
Double_t x; Double_t y;
Double_t x_time; Double_t y_time; Double_t delay;

string TopBot() { if (isTop) return "top"; else return "bottom"; }
void Show();

};
void hit::Show() {
cout << "This is a hit on the " << TopBot() << " CRT. \n";
cout << "Fibers: (" << x_fiber << ", " << y_fiber << "). Position: (" << x << ", " << y << "). \n";
cout << "Time: (" << x_time << ", " << y_time << "). Delay: " << delay << ". \n";
}


struct track
{
track(){}
track(hit topHit_i, hit botHit_i) :
topHit(topHit_i),
botHit(botHit_i)
{
x_delay = botHit->x_time - topHit->x_time;
y_delay = botHit->x_time - topHit->x_time;
}

~track(){}

hit * topHit;
hit * botHit;

Double_t topDelay() { return topHit->delay; }
Double_t botDelay() { return botHit->delay; }
Double_t x_delay;
Double_t y_delay;

void Show();
};
void track::Show(){
cout << "Showing track: \n";
cout << "-------------------------------------- \n";
cout << "This is the top hit: \n";
topHit->Show();
cout << "-------------------------------------- \n";
cout << "This is the bottom hit: \n";
botHit->Show();
cout << "-------------------------------------- \n";
cout << "The overall x delay is " << x_delay << ". \n";
cout << "The overall y delay is " << y_delay << ". \n";
}

void test()
{

hit bruce(1,21,77,13,15);
bruce.Show();

hit giles(0,52,99,14,17);
giles.Show();

jerome = new track(bruce,giles);
jerome->Show();
}



When I run this, it shows bruce and giles just fine, but then when I make jerome out of them, it won't show them properly anymore. It runs hit::Show() but it gives me a bunch of junk numbers in each spot.

So, something is bad about the way I'm accessing hits from track, but I am new at this and am not sure what is wrong. Can somebody point me in the right direction?
I don't see how this compiles.. topHit is a pointer, topHit_i is not. You can't simple make one from another..
Solution 1:
make topHit an object instead of a pointer.
Solution 2:
make the arguments of track constructor pointers. Then you would call it as track(&bruce,&giles)
Solution 3:
make the arguments of track constructor references and apply & to them as you initialise your pointers.
Ahhh, thank you so so much. Solution 2 worked. Finally. I'd been stressing for so long.
Topic archived. No new replies allowed.