hi there,
I have an issue when overloading <<. -
string operator << (room * ¤tPlayerLocation);
-
-
-
string room::operator << (room * ¤tPlayerLocation)
-
{
-
return roomDescription;
-
-
}
-
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?
8 1701
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&);"
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 * ¤tPlayerLocation);
if so, it doesent work. It puts out error message that it has too many parameters.
Ive played around with various formats with this method.
Surely there has to be a way
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: - ostream& operator<<(ostream& out, const clock& myClock) {
-
out << myClock.hours << ":" << myClock.minutes << ":" << myClock.seconds;
-
return out;
-
}
Now, when I write
what is actually happening is more like this: - 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.
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.
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: -
ostream& operator << (ostream &out, const room * ¤tPlayerLocation)
-
{
-
out << currentPlayerLocation->roomDescription;
-
return out;
-
}
Well, I don't think you need to use a pointer: it can just be - ostream& operator << (ostream &out, const room ¤tPlayerLocation);
Also, is roomDescription a private data member? If so, this should be a friend function: - friend ostream& operator << (ostream &out, const room ¤tPlayerLocation)
defined in the room header file.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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)
|
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; }
|
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...
|
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...
|
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....
|
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...
|
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++...
|
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,...
|
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...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |