473,569 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading <<

21 New Member
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 << currentPlayerLo cation;

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

What am I doing wrong?
Oct 30 '07 #1
8 1716
Ganon11
3,652 Recognized Expert Specialist
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&);"
Oct 30 '07 #2
Micko1
21 New Member
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.
Oct 30 '07 #3
Micko1
21 New Member
Ive played around with various formats with this method.

Surely there has to be a way
Oct 30 '07 #4
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 30 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Oct 30 '07 #6
Micko1
21 New Member
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. }
Oct 30 '07 #7
Ganon11
3,652 Recognized Expert Specialist
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.
Oct 30 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Oct 31 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1284
by: Tatu Portin | last post by:
1: #include <iostream> 2: 3: typedef struct { 4: double r; 5: double i; 6: } complex; .. .. .. 24: ostream & operator<< (ostream &str, const complex &a)
10
1625
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
1318
by: pmatos | last post by:
Hi all, I'm having a problem and for illustration purposes I developed code that shows what the problem is about. However, any comment on the code which is not directly about this issue is surely welcome. I have 3 classes, A is abstract, B and C inherit from A and then I create a vector with B's and C's and print them. Of course, I overload...
2
1300
by: zeroYouMustNotSpamtype | last post by:
Hi, Describing this problem will be a bit long winded, but please bear with me: I've got three files in my project: permuts.h, permuts.cpp, and braids.cpp (some content from wich will eventually be moved into braids.h). Both cpp files include the h file. Permuts.h contains the Permutation class, for which I needed to overload <<. Doing...
3
1634
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion. My simplified code looks like : create or replace function orastringcmp (varchar, varchar) returns boolean as 'declare
1
2122
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T...
6
1488
by: Peter v. N. | last post by:
Hi all, Maybe this has been asked a million times before. In that case I'm sorry for being to lazy to search the Internet or look it up in a decent C++ reference: I read in O'Reilly's C++ Pocket reference (see also http://www.oreilly.com/catalog/cpluspluspr/errata/ , btw not that bad), that the operator << has to be overloaded like this:
7
2169
by: Ook | last post by:
The following code compiles and runs. In my overloaded operator<<, I call Parent.stuff. I would expect it to call Child.stuff, since Child is the child class, but it does not. What am I missing, and how can I get it to call Child.stuff. Eventually I'll have different child classes, and I need it to call stuff() from the associated child...
3
1719
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I have defined the < and operators; bool Date::operator<(const Date& d) {
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.