Operator Overloading

Hello everyone!

I am currently writing some code in order to add and subtract vectors. However, I am having some problems with the operator overloading part of the code. Can someone tell me what I am doing wrong please thanks for any help in advance!

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
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
using namespace std;

class twoDvector {
private:
	double dx;
	double dy;
public:
	double getX() const {return dx;}
	double getY() const {return dy;}
	void setData(int x, int y) {x=dx; y=dy;}
	twoDvector operator +(const twoDvector &right);
	twoDvector operator -(const twoDvector &right);
	twoDvector(int x=1, int y=1) {setData(x,y);}
};

twoDvector twoDvector::operator +(const twoDvector &right)
{
	twoDvector tempVec;
	tempVec.dx = dx + right.dx;
	tempVec.dy = dy + right.dy;
	return tempVec;
}

twoDvector twoDvector::operator -(const twoDvector &right)
{
	twoDvector tempVec2;
	tempVec2.dx = dx + right.dx;
	tempVec2.dy = dy + right.dy;
	return tempVec2;
}


int main()
{
	twoDvector vector1(2,2), vector2(3,3);
	twoDvector vector3;

	vector3 = vector1+vector2;
	cout << "The contents of vector 3 is " << vector3;
	cout << endl << endl;
}
Last edited on
You haven't overloaded operator<<. The compiler doesn't know what to do with this statement:

cout << "The contents of vector 3 is " << vector3;

Take a look at this:

http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/10.19.OpOverload/html/text.html#an_important_use_of_operator_overloading


thanks for the help!
Topic archived. No new replies allowed.