Calling a function in another class

Discussion in 'C++' started by John J, May 7, 2004.

  1. John J

    John J Guest

    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;

    }
    John J, May 7, 2004
    #1
    1. Advertising

  2. "John J" <...@...> wrote in message
    news:...
    >
    > 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
    John Harrison, May 7, 2004
    #2
    1. Advertising

  3. John J

    JKop Guest

    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
    JKop, May 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    478
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    438
  3. Satish Chimakurthi

    Function calling another function

    Satish Chimakurthi, Mar 1, 2004, in forum: Python
    Replies:
    2
    Views:
    373
    Satish Chimakurthi
    Mar 1, 2004
  4. Replies:
    5
    Views:
    412
    Phil Winder
    Aug 6, 2006
  5. Jon Clements
    Replies:
    3
    Views:
    316
    geremy condra
    Aug 6, 2010
Loading...

Share This Page