Reputation:
I received an error message when compiling the operator-(double value) function code shown below. The code is merely to find The distance of a point from the origin. Please enlighten me on where i've gone wrong and show me how you resolve it. Let me know if you require more info. Thanks!
Compilation error msg:
Point.cpp: In member function ‘CS170::Point CS170::Point::operator-
(double)’:
Point.cpp:187:49: error: no matching function for call to
‘CS170::Point::Point(double)’
return Point(sqrt(((x * value) + (y * value))));
^
The code is used to achieve this in the driver file:
pt3 = pt1 - 2;
Point Point::operator-(double value)
{
Point temp;
temp=sqrt(((x * value) + (y * value)));
return temp ;
}
//list.h file
class Point
{
public:
// Constructors (2)
explicit Point(double x, double y);
Point();
double getX() const;
double getY() const;
Point operator+(const Point& other)const ;
Point& operator+(double value);
Point operator*(double value) ;
Point operator%(double angle);
double operator-(const Point& other)const ;
Point operator-(double value);
Point operator^(const Point& other);
Point operator+=(double value);
Point& operator+=(const Point& other) ;
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
Point& operator-();
// Overloaded operators (14 member functions)
friend std::ostream &operator<<( std::ostream &output, const Point
&point );
friend std::istream &operator>>( std::istream &input, Point
&point );
// Overloaded operators (2 friend functions)
private:
double x; // The x-coordinate of a Point
double y; // The y-coordinate of a Point
// Helper functions
double DegreesToRadians(double degrees) const;
double RadiansToDegrees(double radians) const;
};
// Point& Add(const Point& other); // Overloaded operators (2 non-member,
non-friend functions)
// Point& Multiply(const Point& other);
Point operator+( double value, const Point& other );
Point operator-( double value, const Point& other );
Upvotes: 0
Views: 169
Reputation: 41
You need to make a Point
Constructor that takes a double
parameter.
Point (double d){
//whatever logic of point construction.
};
to solve the error in line.
temp=sqrt(((x * value) + (y * value)));
But that would end up constructing a point like.
Point P = 5;
some where else and you may not want that to happen.
in your shoes I would make it explicit constructor.
explicit Point(double d){
//whatever logic of point construction.
};
that way you would end up initializing your point this way needing explicit casting from double
to Point
Point P1 = (Point)5;
Point P2 = (Point)sqrt(((x * value) + (y * value)));
and finally I'll argue about the Point - double
subtraction logic you are doing in your function.
Upvotes: 0
Reputation: 73595
Your Point
class constructor takes two parameters, x
, and y
, whereas the result of sqrt
is a single value. If you want to use the same value twice, then either make a constructor which accepts a single value, or assign the result of sqrt
to a variable, and then pass that variable into the constructor twice.
Upvotes: 1