I'm trying to add a multiplication operator to a Point class. I'm still getting two errors when I try and compile it. Can someone point me in the right direction?
#include <iostream>
usingnamespace std;
class Point {
private: // Data members (private)
int x, y;
public: // Constructors
Point() {set(0,0);}
Point(int new_x, int new_y) {set(new_x, new_y);}
Point(const Point &src) {set(src.x, src.y);}
// Operations
Point add(const Point &pt);
Point sub(const Point &pt);
Point mul(const Point &pt1, const Point &pt2);
Point operator+(const Point &pt) {return add(pt);}
Point operator-(const Point &pt) {return sub(pt);}
Point friendoperator*(const Point &pt1, const Point &pt2) {return mul(pt1,pt2);};
// Other member functions
void set(int new_x, int new_y);
int get_x() const {return x;}
int get_y() const {return y;}
};
int main() {
Point point1(20, 20);
Point point2(0, 5);
Point point3(-10, 25);
Point point4 = 2 * point2;
cout << "The point is " << point4.get_x();
cout << ", " << point4.get_y() << "." << endl;
system("PAUSE");
return 0;
}
void Point::set(int new_x, int new_y) {
if (new_x < 0)
new_x *= -1;
if (new_y < 0)
new_y *= -1;
x = new_x;
y = new_y;
}
Point Point::add(const Point &pt) {
Point new_pt;
new_pt.x = x + pt.x;
new_pt.y = y + pt.y;
return new_pt;
}
Point Point::sub(const Point &pt) {
Point new_pt;
new_pt.x = x - pt.x;
new_pt.y = y - pt.y;
return new_pt;
}
Point Point::mul(const Point &pt1, const Point &pt2){
Point new_pt;
new_pt.x = pt2.x * pt1.x;
new_pt.y - pt2.y * pt1.y;
return new_pt;
}
Here are the errors I'm getting when compiling with Code::Blocks
C:\CProgramming\cb test.cpp||In function `Point operator*(const Point&, const Point&)':|
C:\CProgramming\cb test.cpp|17|error: cannot call member function `Point Point::mul(const Point&, const Point&)' without object|
C:\CProgramming\cb test.cpp||In function `int main()':|
C:\CProgramming\cb test.cpp|28|error: no match for 'operator*' in '2 * point2'|
C:\CProgramming\cb test.cpp|17|note: candidates are: Point operator*(const Point&, const Point&)|
||=== Build finished: 2 errors, 0 warnings ===|
Hi
I think there are two main things wrong here. Point point4 = 2 * point2;
You'll want this to call your operator* function, which expects two Point objects. But the first argument it would get is an int, and your Point class has no way of converting that into a Point. One way to fix that would be to have a conversion function (- a single argument constructor):
Point::Point( int n ): x(n), y(n) {}
(Assuming you want both x and y to be initialized with that given value.)
Secondly: Point friendoperator*(const Point &pt1, const Point &pt2) {returnmul(pt1,pt2);};
You're trying to call mul() on an implicit object, i.e. this->mul(pt1,pt2)
But this is a non-member friend function - you don't call it on an object of type Point so there's no implicit object on which to call further member functions. You'll need to call mul() on either of your parameters (pt1 or pt2) and rewrite it (mul()) to take one parameter: Point Point::mul( const Point& p )
Hope that helps.
Hi @Zexanima
Actually I should also have said: if you look inside your operator* function, all it does is call a public member function of your Point class, so in the interests of minimizing outside access to your class's internals, this should really be a non-member non-friend function. Same for operator+ and operator-.
Regards, keineahnung :)
Just FYI, multiplying points doesn't make much sense. If you wanted to return the dot product (inner product) of the two, you would return a real number, not a point. Also, why are your points' components defined as ints? Unless they can only exist on integer coordinates like pixels (which would be unsigned anyway) you probably want doubles.
This was just to test out and get use to the syntax of classes. This program has no real purpose really. I know it doesn't make sense. The exercise didn't say to add a multiplication operator either but I just did it for extra practice.
We'll then it might be a good example to show that just because an operation is p * q (p and q are Points) doesn't mean it needs to return a point also. Example:
1 2 3
int /*double*/ operator*(const Point &p, const Point &q) {
return p.get_x() * q.get_x() + p.get_y() * q.get_y();
}