operator << and >> another way...

Hello! I got definitions of >> and << operator:

1
2
3
4
5
ostream& operator<<(ostream& out, Vector2D& vector)
{
	out<<"X "<<vector.x<<endl<<"Y: "<<vector.y;
	return out;
}


1
2
3
4
5
6
7
8
istream& operator>>(istream& in, Vector2D& vector)
{
  cout<<"X: ";
  in>>vector.x;
  cout<<"Y: ";
  in>>vector.y;
  return in;
}


but i must use in definition of this operators functions Get:
1
2
3
4
5
6
7
void Vector2D::Get(istream& in)
{
	cout<<"X: ";
	in>>x;
	cout<<"Y: ";
	in>>y;
}


and Put:
1
2
3
4
5
void Vector2D::Put(ostream& out)
{
	out<<"X : "<<x; cout<<endl;
	out<<"Y "<<y; cout<<endl;
}


i don't know how to do this because functions Put and Get should return void...
can somebody explain me how to do this?
Last edited on
1
2
3
4
5
ostream& operator << ( ostream& outs, const Vector2D& vector )
  {
  vector.Put( outs );
  return outs;
  }
(Don't forget the const in the insertion operator signature above.)

The code for the extraction operator is similar:
1
2
3
4
5
istream& operator >> ( istream& ins, Vector2D& vector )
  {
  vector.Get( ins );
  return ins;
  }

Hope this helps.
Last edited on
It helps me so much, thanx!
Topic archived. No new replies allowed.