Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading <<

Newbie
 
Join Date: Sep 2007
Location: Australia
Posts: 21
#1: Oct 30 '07
hi there,

I have an issue when overloading <<.

Expand|Select|Wrap|Line Numbers
  1.     string operator << (room * &currentPlayerLocation);
  2.  
  3.  
  4. string room::operator << (room * &currentPlayerLocation)
  5. {
  6.     return roomDescription;
  7.  
  8. }
  9.  
when I call this function:

cout << currentPlayerLocation;

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

What am I doing wrong?

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 30 '07

re: overloading <<


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&);"
Newbie
 
Join Date: Sep 2007
Location: Australia
Posts: 21
#3: Oct 30 '07

re: overloading <<


Quote:

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<<(string&, room const * &currentPlayerLocation);

if so, it doesent work. It puts out error message that it has too many parameters.
Newbie
 
Join Date: Sep 2007
Location: Australia
Posts: 21
#4: Oct 30 '07

re: overloading <<


Ive played around with various formats with this method.

Surely there has to be a way
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#5: Oct 30 '07

re: overloading <<


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:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& out, const clock& myClock) {
  2.    out << myClock.hours << ":" << myClock.minutes << ":" << myClock.seconds;
  3.    return out;
  4. }
Now, when I write

Expand|Select|Wrap|Line Numbers
  1. cout << myClock << endl;
what is actually happening is more like this:

Expand|Select|Wrap|Line Numbers
  1. operator<<(myClock.operator<<(cout, myClock), endl);
Your operator<< cannot return a string nor expect a string argument if it is to properly overload the << operator.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#6: Oct 30 '07

re: overloading <<


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.
Newbie
 
Join Date: Sep 2007
Location: Australia
Posts: 21
#7: Oct 30 '07

re: overloading <<


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:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator << (ostream &out, const room * &currentPlayerLocation)
  2. {
  3.     out << currentPlayerLocation->roomDescription;
  4.     return out;
  5. }
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#8: Oct 31 '07

re: overloading <<


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

Expand|Select|Wrap|Line Numbers
  1. ostream& operator << (ostream &out, const room &currentPlayerLocation);
Also, is roomDescription a private data member? If so, this should be a friend function:

Expand|Select|Wrap|Line Numbers
  1. friend ostream& operator << (ostream &out, const room &currentPlayerLocation)
defined in the room header file.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#9: Oct 31 '07

re: overloading <<


Quote:

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.
Reply