Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-05-2005, 07:00 AM   #1
Member (7 bit)
 
shaticus's Avatar
 
Join Date: May 2004
Posts: 103
Operator Overloading (C++)

For a computer science project I have to overload a few operators in C++. We are supposed to make them friends of the class we are writting. Does anyone know of a good tutorial on operator overloading on the ostream?

I am trying to do something like this:

class book{
friend ostream &operator<<(ostream&, book&);

};


ostream &operator<<(ostream &output , book &bookOut)
{
output << "text\n";

return output;
}

When I try to compile it tells me that ostream is incomplete and wants a , before the & in the declaration friend ostream &operator<<(...)
What is wrong with my code, or is it something with the compiler?


Thanks

Last edited by shaticus; 05-05-2005 at 11:55 AM.
shaticus is offline   Reply With Quote
Old 05-06-2005, 05:00 AM   #2
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
looks fine to me, although some compilers dont like the & ampersand
on the rightside.

Borland C++ likes it this ways:
friend ostream& operator<<(ostream&, book&);
-------------^
-------------+------------& is here instead


------------------------------------
some compilers wanted:

friend std:: ostream &operator<<( std:: ostream&, book&);
then.....
std:: ostream &operator<<( std:: ostream, book&);

Notice the "std::" there is no space between std:: and ostream!
I have to do this because this forum formater makes smilies out of the them!

------------------------------------

if your need more references try these pages:

overloading
http://www.cplusplus.com/doc/tutorial/tut4-2.html

friend
http://www.cplusplus.com/doc/tutorial/tut4-3.html

hope this helps a bit

Last edited by alfie2; 05-06-2005 at 06:22 AM.
alfie2 is offline   Reply With Quote
Old 05-06-2005, 06:30 AM   #3
Member (7 bit)
 
shaticus's Avatar
 
Join Date: May 2004
Posts: 103
Thanks, that fixed it!
shaticus is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:22 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2