By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Calling a function in another class

P: n/a

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;

}


Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"John J" <...@...> wrote in message
news:af******************************@news.teranew s.com...

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
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.