Apr 23, 2010 at 9:06pm UTC
Last edited on Apr 23, 2010 at 9:14pm UTC
Apr 23, 2010 at 9:11pm UTC
Something like this?
1 2 3 4
ostream & operator <<( ostream & out, const Vector2D & obj ) {
out << "(" << obj.x << ", " << obj.y << ")" ; // send individual values to out
return out; // return ostream so that calls may be chained (<< a << b << c)
}
Last edited on Apr 23, 2010 at 9:14pm UTC
Apr 23, 2010 at 9:16pm UTC
Those aren't pointers but references
Then why does one find the reference operator under "pointers"?
-Albatross
Last edited on Apr 23, 2010 at 9:17pm UTC
Apr 23, 2010 at 9:17pm UTC
Nope, they may be implemented as pointers but that is not how they are exposed to the C++ programmer
Apr 23, 2010 at 9:18pm UTC
Mistype, I got my terminology mixed up. I meant what you see above now.
-Albatross
Apr 23, 2010 at 10:26pm UTC
it doesn't work because it's not the same function ;)
the one in your class definition is:
ostream& operator <<(ostream& out, Vector vector);
<- here you pass Vector
and the one you declare is:
ostream& operator <<(ostream& out, Vector& vector);
<- here you pass Vector &
just add a '&' in the one in your class definition and it should be fine
Last edited on Apr 23, 2010 at 10:27pm UTC
Apr 23, 2010 at 10:33pm UTC
HAAAA!! it's working:D thank you...how stupid i'm!!
Apr 23, 2010 at 10:53pm UTC
Ehh...sorry, but i have one more question... how can I make functions Put(istream&) and Get(ostream&) ? i did something like that:
in declaration:
1 2 3 4 5 6 7 8 9 10 11
class Vector
{
protected :
int x, y;
public :
Vector();
virtual void Get(istream& in);
virtual void Put(ostream& out);
.
.
.
in definition:
1 2 3 4 5 6 7 8 9 10 11 12
void Vector::Get(istream& in)
{
cout<<"X: " ;
in<<this ->x;
cout<<"Y: " ;
in>>this ->y;
}
void Vector::Put(ostream& out)
{
out<<"X : " <<this ->x<<endl;
out<<"Y : " <<this ->y<<endl;
}
and it's not working... can't compile...
binary '<<' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
could be 'std::ostream &operator <<(std::ostream &,Vector &)'
can you help me one more time?
Last edited on Apr 23, 2010 at 10:54pm UTC
Apr 23, 2010 at 11:00pm UTC
your compiler is complaining about this:
in<<this ->x;
it should be like this:
in>>this ->x;
:P
also, since Get and Put are member functions, you don't have to use this
to access the members, just write plain x and y.
Last edited on Apr 23, 2010 at 11:05pm UTC