overloading <<

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Micko1
    New Member
    • Sep 2007
    • 21

    #1

    overloading <<

    hi there,

    I have an issue when overloading <<.

    Code:
    	string operator << (room * &currentPlayerLocation);
    
    
    string room::operator << (room * &currentPlayerLocation)
    {
    	return roomDescription;
    
    }
    when I call this function:

    cout << currentPlayerLo cation;

    Its cout's the actual memory address - 00346380 instead of the string roomDescription .

    What am I doing wrong?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    From www.cplusplus.com, the function header for the ostream's << operator is:

    "ostream& operator<< (ostream& os, const string& str);"

    So, to overload it, you need to make a function like:

    "ostream& operator<< (ostream&, const yourClass&);"

    Comment

    • Micko1
      New Member
      • Sep 2007
      • 21

      #3
      Originally posted by Ganon11
      From www.cplusplus.com, the function header for the ostream's << operator is:

      "ostream& operator<< (ostream& os, const string& str);"

      So, to overload it, you need to make a function like:

      "ostream& operator<< (ostream&, const yourClass&);"
      thankyou for the reply.

      so you say to make a function like:

      "ostream& operator<< (ostream&, const yourClass&);

      would this equate to:

      string& operator<<(stri ng&, room const * &currentPlayerL ocation);

      if so, it doesent work. It puts out error message that it has too many parameters.

      Comment

      • Micko1
        New Member
        • Sep 2007
        • 21

        #4
        Ive played around with various formats with this method.

        Surely there has to be a way

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          No, a string is nothing like an ostream. Your function has to accept an ostream parameter, use that ostream operator to output whatever portions of your class you wish to be seen, and then return that ostream variable. For example, if I had a clock class, my operator<< function might look like:

          [CODE=cpp]ostream& operator<<(ostr eam& out, const clock& myClock) {
          out << myClock.hours << ":" << myClock.minutes << ":" << myClock.seconds ;
          return out;
          }[/CODE]

          Now, when I write

          [CODE=cpp]cout << myClock << endl;[/CODE]

          what is actually happening is more like this:

          [CODE=cpp]operator<<(myCl ock.operator<<( cout, myClock), endl);[/CODE]

          Your operator<< cannot return a string nor expect a string argument if it is to properly overload the << operator.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Add to Ganon11's comments that since the operator<< must have an ostream& as a first argument, it cannot be a member function. (Recall that member functions have the this pointer as a hidden first argument). Therefore, if your operator<< needs access to class private data, it will need to be a friend function.

            Comment

            • Micko1
              New Member
              • Sep 2007
              • 21

              #7
              Thankyou both for the reply.

              I misunderstood the definition for the overloading teplate, I have made the changes and I'm still getting the memory address.

              I think it is because I am using a pointer:

              Code:
              ostream& operator << (ostream &out, const room * &currentPlayerLocation)
              {
              	out << currentPlayerLocation->roomDescription;
              	return out;
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Well, I don't think you need to use a pointer: it can just be

                [CODE=cpp]ostream& operator << (ostream &out, const room &currentPlayerL ocation);[/CODE]

                Also, is roomDescription a private data member? If so, this should be a friend function:

                [CODE=cpp]friend ostream& operator << (ostream &out, const room &currentPlayerL ocation)[/CODE]

                defined in the room header file.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by Micko1
                  I think it is because I am using a pointer:
                  You weren't using a pointer. You were using a reference to a pointer. That's equivalent to using a pointer to a pointer.

                  Comment

                  Working...