# 运算符重载函数

##### 问题描述投票：0回答：2

``````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))));
^
``````

`pt3 = pt1 - 2;`

``````  Point Point::operator-(double value)
{

Point temp;
temp=sqrt(((x * value) + (y * value)));
return temp ;

}
``````

//list.h文件

`````` 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;
};

// 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 );
``````
##### 2个回答
1

`Point`类的构造函数有两个参数，`x``y`，而`sqrt`的结果是一个值。如果你想使用相同的值的两倍，那么无论做它接受单个值的构造函数或`sqrt`的结果分配给一个变量，然后传递变量到构造函数的两倍。

0

``````Point (double d){
//whatever logic of point construction.
};
``````

``````temp=sqrt(((x * value) + (y * value)));
``````

``````Point P = 5;
``````

``````explicit Point(double d){
//whatever logic of point construction.
};
``````

``````Point P1 = (Point)5;
Point P2 = (Point)sqrt(((x * value) + (y * value)));
``````