[Operator Overloading] Finding the product of two class objects

I have an assignment that requires me to overload the * operator to find the product of two objects of the same class type. The class has two int member member variables, x and y. The product of the two classes should be calculated as (first X * second X) + (first Y * second Y). I have overloaded the * operator within the class based on examples I've seen, but I'm getting the following compiler error:

lab4.cpp:26: no matching function for call to 'Vector2D::Vector2D (int)'

I am not sure what this means. I will paste my code below, and I would appreciate any suggestions.

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
44
45
46
47
48
49
50
51
#include <iostream>

class Vector2D
{
public:
	Vector2D()
	{
		x = 0;
		y = 0;
	}
	int get_x()
	{
		return x;
	}
	int get_y()
	{
		return y;
	}
	void set_values(int new_x, int new_y)
	{
		int x = new_x;
		int y = new_y;
	}
	const Vector2D operator*(const Vector2D &RHS)
	{
		return Vector2D((x * RHS.x) + (y * RHS.y));
	}
private:
	int x;
	int y;
};

int main()
{
	Vector2D myVector, yourVector;

	myVector.set_values(3, 7);
	yourVector.set_values(12, 8);

	Vector2D theirVector = myVector * yourVector;

	std::cout << "My vector has the values " << myVector.get_x() << " and " 
			  << myVector.get_y() << "." << endl;
	std::cout << "Your vector has the values " << yourVector.get_x() << " and " 
			  << yourVector.get_y() << "." << endl;
	std::cout << "Their vector has the values " << theirVector.get_x() << " and " 
			  << theirVector.get_y() << ", which is the dot product of my vector"
			  << " and your vector.";

	return 0;
}
closed account (D80DSL3A)
The culprit: return Vector2D((x * RHS.x) + (y * RHS.y));
Actually, the way you describe the sum:
The product of the two classes should be calculated as (first X * second X) + (first Y * second Y)
that is an integer value. If that's so, then your operator* should return an int, not a const Vector2D, then
return (x * RHS.x) + (y * RHS.y); instead.
I see, that makes sense. Thank you for the insight!
Topic archived. No new replies allowed.