运算符重载模函数

问题描述 投票:0回答:1

我应该做模函数的过载,但我不知道该怎么做。让我知道如果你需要更多的信息。

这是我的学校我的要求:

成员函数旋转的有关度的指定数量的原点。返回一个新的点

里面的驱动程序文件,我校希望模函数来完成这样的情景:

点PT1(-50,-50); 双角= 45; 点PT2 = PT1%角;

这是我已经试过:

Point Point::operator%( int value)
{
    (int)x%value;

    (int)y%value;

    return *this;
}

//point.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%(int value);

   Point 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 );
c++ operator-overloading
1个回答
1
投票

我看到的第一个错误是,你不尊重你的assignement的要求。你assignement专门请教您类型支持此操作:

Point pt1{-50, 50};
Point pt2 = pt1 % 45.5;

由此推断你的运营商必须与操作返回一个点用双适用于它。很显然你存储的角度为双,但收到一个int。这是不尊重您的要求。此外,您返回一个旋转的点,但不正确的。你让做一个新点的操作的就地操作来代替。您的运营商里面,你应该创建一个新的位置的点。事情是这样的:

Point Point::operator%(double) const {
    return Point{..., ...};
}

然后,你的操作是错误的。你投点数据成员intonly做对他们模。取模不做旋转。的旋转通常与正弦和余弦进行。不能使用上的int C ++ %operator进行旋转。

© www.soinside.com 2019 - 2024. All rights reserved.