Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling a function in another class

Reply
Thread Tools

Calling a function in another class

 
 
John J
Guest
Posts: n/a
 
      05-07-2004

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;

}






 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-07-2004

"John J" <...@...> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      05-07-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a class' memver function from inside another class philipwinder@googlemail.com C++ 5 08-06-2006 08:42 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Function calling another function Satish Chimakurthi Python 2 03-01-2004 11:52 PM



Advertisments