|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (7 bit)
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. |
|
|
|
|
|
#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. |
|
|
|
|
|
#3 |
|
Member (7 bit)
Join Date: May 2004
Posts: 103
|
Thanks, that fixed it!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|