overloading operators<<>>, virtual functions

All that i have to do is write class Vector2D and then implement some methods:

1
2
3
4
5
6
7
8
class Vector2D {
	double x,y;
	virtual double GetLength();
	virtual void Put(ostream& out);
	virtual void Get(istream& in);
	friend operator<<(ostream&, Vector2D&)
	friend operator>>(istream&, Vector2D&)
}


I don't know what means this ostream&...istream&...Can somebody explain me how to do this?
Last edited on
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
Those aren't pointers but references ( http://www.cplusplus.com/doc/tutorial/functions2/ )
Streams must be passed by reference ( or pointer ) since they aren't copyable
Those aren't pointers but references

Then why does one find the reference operator under "pointers"?

-Albatross
Last edited on
Nope, they may be implemented as pointers but that is not how they are exposed to the C++ programmer
Mistype, I got my terminology mixed up. I meant what you see above now.

-Albatross
my code now looks like that:
.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

class Vector
{
	protected:
		int x, y;
	public:
		Vector();				
		friend ostream& operator<<(ostream& out, Vector vector);
		~Vector();
};

.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Przyklad.h"

using namespace std;

Vector::Vector():x(3), y(2)
{}

ostream& operator<<(ostream& out, Vector& vector)
{
	out<<"X: "<<vector.x<<endl<<"Y: "<<vector.y;
	return out;
}

Vector::~Vector()
{}


but this friend function can't acces x and y...what should i do?
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
HAAAA!! it's working:D thank you...how stupid i'm!!
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
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
Topic archived. No new replies allowed.