![]() |
|
#1
|
||||
|
||||
Operator Overloading: <<I recently discovered the world of operator overloading, but I haven't had any formal instruction on it, and it's actually a bit ahead of where I am right now, but I feel that it would be a good addition to a List class I recently created. I want to overload the << operator for the List class so that I can display the list on the screen using cout. I've read a few tutorials, but none of them are entirely clear or they are entirely vague. So, what I am asking is for someone to give me an example of an overloaded << operator, specifically for use with an ostream and cout. This would be something for me to study and learn from so that I could implement it in my List class, so for anyone who does decide to help out, please don't skip important details
__________________
-Aaron |
||||
|
#2
|
||||
|
||||
|
wanna to tell u the detail is quite hard
not sure u will understand it i give u a website u go to refer it ok 8-) http://www.cplusplus.com/search/sear...or+Overloading bottom this is the example i give u hopefully u can understand it if still cannot understand it i will give u more detail __________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#3
|
||||
|
||||
|
Well, I understand how they work and what they're used for, I just need help implementing them.
EDIT: I have already been to that website. I did a pretty extensive browsing of google for overloading tutorials, but I didn't find much good stuff. I wrote this code the other night, but it produces nothing but errors: CPP / C++ / C Code:
__________________
-Aaron |
|
#4
|
||||
|
||||
|
u try put a reference(&) at here
ostream& List: __________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#5
|
||||
|
||||
|
Okay, well I gave up on the stream operator and went for an easier one... the = operator! I successfully implemented it in my List class, and now I can use it to copy a char array implicitly to my List. You know, it really surprises me that I didn't get any responses from dsmith or WaltP... I felt like I was starting to get a little respect around here, but I guess not. Thanks for the replies, tay!
__________________
-Aaron |
|
#6
|
|||
|
|||
|
don't give up on it, maybe if you could post the entire program i could test it and then see whats wrong, but otherwise your original posted code seems alright.
|
|
#7
|
||||
|
||||
|
Well, most of the errors it gave me seemed to indicate that the compiler didn't know what an ostream was. I included <iostream>, though... so I dunno what the heck its problem was.
__________________
-Aaron |
|
#8
|
|||
|
|||
|
Quote:
this seems like a stupid question but did you include fstream? |
|
#9
|
|||
|
|||
|
aha, i think i know why. Did you know that stream operators must be friend functions? they cannot be members of a class.
|
|
#10
|
||||
|
||||
|
I didn't include fstream, and I have read a little about friend functions. So, if I say it's a friend of my List class, can I use all of my private variables from the list class as if I were dealing with a this object? I mean, since the operator will not be overloaded for the class, the function isn't loaded within the class, and therefore the private data is inaccessible. I'll try it out and see what happens while I wait for a response. Thank you, machinated
edit: I do know that I only need to say the word "friend" in the function proto-type, and not in the actual definition of the function, so don't worry about that. __________________
-Aaron |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The