【问题标题】:Operator overloading functions运算符重载函数
【发布时间】:2019-02-04 08:57:10
【问题描述】:

我对 2 个非成员、2 个非友元乘法和运算符重载函数的添加感到困惑。我不确定该怎么做。有人可以帮助我解决这个问题吗?请参阅下面的代码。先感谢您!

编译器输出:

Point.cpp:208:19: error: passing ‘const CS170::Point’ as ‘this’ argument discards qualifiers [-fpermissive]

    return other + value;
                   ^~~~~

Point.cpp: In function ‘CS170::Point CS170::

operator*(double, const CS170::Point&)’:

Point.cpp:215:10: error: ‘double CS170::Point::x’ is private within this context

   result.x = value * x;
          ^

Point.cpp:215:22: error: ‘x’ was not declared in this scope

   result.x = value * x;
                      ^
Point.cpp:216:10: error: ‘double CS170::Point::y’ is private within this context

   result.y =  value * y;
          ^

Point.cpp:216:23: error: ‘y’ was not declared in this scope
   result.y =  value * y;

点.h

  #include <iostream> // istream, ostream

  namespace CS1100
{
  class Point
  {
   public:
  // Point(double X, double Y);    // Constructors (2)
  explicit Point(double x, double y); 

   Point();

   Point operator+(const Point& other)const ;

   Point& operator+(double value);


   Point operator*(double value) ;

   Point operator%(double 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 );

我的源代码:

///////////////////////////////////////////////////////////////////////////////
// 2 non-members, non-friends (operators)


double operator+( double value, const Point& other ) 
{
     return other + value;
}   

double operator*( double value, const Point& other ) 
{

    Point result;
    result.x = value * x;
    result.y =  value * y;
    return result;
}   

【问题讨论】:

    标签: c++ operator-overloading


    【解决方案1】:

    据我了解对问题的讨论,问题实际上并不在于操作员本身,而是允许的成员函数的数量受到限制——而您已经超过了这个限制。

    但是,您有很多不需要成为成员的函数,例如:

    class Point
    {
    public:
        Point operator+(const Point& other) const
        {
            return Point(x + other.x, y + other.y);
        }
    };
    

    从所有这些中创建免费功能:

    class Point { /*...*/ };
    
    Point operator+(Point const& l, Point const& r)
    {
        return Point(l.getX() + r.getX(), l.getY() + r.getY());
    }
    

    像上面显示的那样移出所有这些运算符后,您就离限制足够远了,因此您可以引入所需的 getter:

    class Point
    {
    public:
        double getX() { return x; };
        double getY() { return y; };
    };
    

    如果您愿意重命名成员变量,例如。 G。通过添加前缀,您可以遵循另一种模式:

    class Point
    {
        double m_x, m_y;
    public:
        double x() { return m_x; };
        double y() { return m_y; };
    
        void x(double v) { m_x = v; }; // the corresponding setter
                                       // (for illustration, you might not need it)
    };
    

    后一种模式也很常见。跳过显式 getset 前缀的优点是更短,缺点正是失去了这种显性......决定你,你喜欢哪个。但是,比个人偏好更重要的是一致性,所以如果有 e。 G。公司的惯例或惯例,遵循那个...

    您的一些操作员将需要保留成员,但是,这些操作员都修改当前对象:

    class Point
    {
    public:
        Point& operator+=(const Point& other) /* const */ // NEEDS to be non-const
        {
            x += other.x;
            y += other.y;
            return *this; // <- very good hint to spot the ones needing to stay members
        }
    };
    

    如果你有一个公共的 copy 构造函数,你可以重新使用operator+= 来定义operator+

    class Point
    {
    public:
        Point(Point const& other) : Point(other.x, other.y) { }
    };
    
    Point operator+(Point const& x, Point const& y)
    {
        Point r(x); // or Point(x.x(), x.y()), if you lack such constructor)
        r += y;
        return r;
    }
    

    实际上,您甚至可以通过按值接受参数之一来节省显式副本:

    Point operator+(Point x, Point const& y)
    //                    ^ no reference
    {
        return x += y;
    }
    

    后者只是为了说明,我更喜欢在给定情况下的两个引用以保持界面的对称性...

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多