GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2004, 16:18
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough

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  
Old 23-Apr-2004, 22:37
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
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  
Old 24-Apr-2004, 01:27
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
ostream& List::operator << ( ostream &out, List inList )
{
  char outList[MAX_LENGTH+1] = {'\0'};
  int  count;

  for (count = 0; count < inList.length; count++)
    outList[count] = inList.data[count].value;

  out << outList;
  return out;
}
__________________
-Aaron
  #4  
Old 24-Apr-2004, 09:35
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
u try put a reference(&) at here
ostream& List:perator << ( ostream &out, List &inList )
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #5  
Old 26-Apr-2004, 18:21
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 26-Apr-2004, 18:28
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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  
Old 26-Apr-2004, 18:31
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 26-Apr-2004, 18:33
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by aaroncohn
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.


this seems like a stupid question but did you include fstream?
  #9  
Old 26-Apr-2004, 18:43
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
aha, i think i know why. Did you know that stream operators must be friend functions? they cannot be members of a class.
  #10  
Old 26-Apr-2004, 20:39
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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 GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.