operator overloading for streams

This is a discussion on operator overloading for streams within the C++ Programming forums, part of the General Programming Boards category; Hi, This one's not so important but, nice to have. I've been playing with the operator overloading for some simple ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    operator overloading for streams

    Hi,

    This one's not so important but, nice to have. I've been playing with the operator overloading for some simple classes and it's quite nice. I found some examples of overloading << which don't work for me though.

    I use this in the header file:

    Code:
    ostream& operator <<(ostream&, const Point2D&);
    
    // the actual operator
    
    ostream & Point2D::operator <<(ostream &os, const & Point2D point) {
    	return os << point.x << "," << point.y << endl;
    }
    I get some compiler error complaining about too many parameters. Although every example I've seen takes two parameters. I get actually get it to compile with only the ostream parameter but it doesn't do anything for me...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    It should not be a member function of Point2D. In general it could either be a member of ostream (the left hand argument) or a non-member function. Since you aren't allowed to change ostream yourself, you must make it a non-member function.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The examples you've seen don't implement it as a class member...

    Code:
    ostream & operator <<(ostream &os, const & Point2D point) {
    	return os << point.x << "," << point.y << endl;
    }
    It's kinda hard to explain... but...
    • When you implement a binary operator as a class member, it assumes that *this is the left side operand, and the paramater is the right hand operand. So, there's no room for a second operand.
    • operator<< is a binary operator

    Look at all your examples, implementing operator<< as a class member only really makes sense if you are making an ostream.

    Hope that makes sense.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    That actually makes sense, thanks for the explanation. It works great now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fstreams
    By Amyaayaa in forum C++ Programming
    Replies: 9
    Last Post: 06-10-2008, 12:00 PM
  2. fstreams
    By Amyaayaa in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 02:08 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM
  5. trying to use fstreams... specifically input.seekg
    By sarahbee in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2002, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21