Writing an ostream << function for a custom class

I'm not having too much luck here. I've written a custom vector class, and I'd to be able to use it with cout's << operator. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <math.h>
using namespace std;

class vec {
	public:
		int x, y;
		vec (int a, int b) {
			x = a;
			y = b;
		}
		vec operator + (vec& o) { return vec(x+o.x, y+o.y); }
		vec operator - (vec& o) { return vec(x-o.x, y-o.y); }
		vec operator * (vec& o) { return vec(x*o.x, y*o.y); }
		vec operator * (int& o) { return vec(x*o, y*o); }

		void operator += (vec& o) { x += o.x; y += o.y; }
		void operator -= (vec& o) { x -= o.x; y -= o.y; }
		void operator *= (vec& o) { x *= o.x; y *= o.y; }
		void operator *= (int& o) { x *= o; y *= o; }

		int dot (vec& o) { return x*o.x + y*o.y; }
		double length (void) { return sqrt( (x*x) + (y*y) ); }
};

ostream& ostream::operator << (vec& val) {
	this << "(" << val.x << ", " << val.y << ")" << endl;
	return this;
}


int main () {
	vec x (3, 4);
	cout << x;
	return 0;
}


What should happen is that '(3, 4)' should be printed to the console. What actually happens is that the code doesn't compile. It gives the error "template-id `operator<< <>' for `std::ostream& std::basic_ostream<char, std::char_traits<char> >::operator<<(vec&)' does not match any template declaration"

Can anybody help me out here?
Topic archived. No new replies allowed.