Calling a function in another class

J

John J

I have written the following overload of operator << as a display function.
In the code I unsuccessfully try and call a function within another class(<<
"Race : " << r->Show () << endl). The Show function is in a class called
Race which is also included below. I'd greatly appreciate some guidance on
what I'm doing wrong.

Thanks for any help

ostream& operator<< (ostream& out, const Entry& e)

{

Race* r;

out << "Yacht : " << *(e.what) << endl

<< "Race : " << r->Show () << endl

<< "Finish Place : " << e.place << endl

<< "Finish Time : " << e.time << endl << endl;


return out;

}

//Show () function within a Race class

void Race::Show (ostream& out) const

{

out << "Race Number : " << number << endl

<< "Race Date : " << date << endl << endl;

}
 
J

John Harrison

John J said:
I have written the following overload of operator << as a display function.
In the code I unsuccessfully try and call a function within another class(<<
"Race : " << r->Show () << endl). The Show function is in a class called
Race which is also included below. I'd greatly appreciate some guidance on
what I'm doing wrong.

What you are doing wrong it not thinking clearly about *which* Race object
you are trying to use to call the Show function. Which Race is it that you
want to Show?
Thanks for any help

ostream& operator<< (ostream& out, const Entry& e)

{

Race* r;

This is a pointer to a race object. But is has not been initialised to point
anywhere.

out << "Yacht : " << *(e.what) << endl

<< "Race : " << r->Show () << endl

Now you are using an uninitialised pointer. I would expect a program crash
at this point.
<< "Finish Place : " << e.place << endl

<< "Finish Time : " << e.time << endl << endl;


return out;

}

//Show () function within a Race class

Hard to know how to fix it because I don't know what you are trying to
achieve.

Presumably each Yacht is associated with a Race in some way (each Yacht is
entered in a Race maybe). Somehow you have to get that Race object that is
associated with the Yacht object that you are trying to display and use that
in your operator<< function. One way would be if each Yacht object had a
Race object as a member variable but it all depends on exactly what you are
trying to do.

The way you've written it, it looks like you are trying to conjure a Race
object out of thin air, that would be perfectly possible, but I doubt it is
what you want to do.

john
 
J

JKop

John J posted:
I have written the following overload of operator << as a display
function. In the code I unsuccessfully try and call a function within
another class(<< "Race : " << r->Show () << endl). The Show function is
in a class called Race which is also included below. I'd greatly
appreciate some guidance on what I'm doing wrong.

Thanks for any help

ostream& operator<< (ostream& out, const Entry& e)

{

Race* r;

out << "Yacht : " << *(e.what) << endl

<< "Race : " << r->Show () << endl

<< "Finish Place : " << e.place << endl

<< "Finish Time : " << e.time << endl << endl;


return out;

}

//Show () function within a Race class

void Race::Show (ostream& out) const

{

out << "Race Number : " << number << endl

<< "Race Date : " << date << endl << endl;

}


"Race : " << Race::Show () << endl


The "Show" function must be declared static in the class declaration.


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Class search function 1
Associated classes 3
Unexpected Result 8
Search function 3
Calling a member function 1
Exceptions in class 7
Get await function in loop to finish before script ends 0
Class problem 5

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top