简体   繁体   English

运算符重载时如何调用另一个成员函数(C ++)

[英]How to call another member function when operator overloading (C++)

How would I use a member function (in this case, magnitude() ) within my definition for the function overload of the greater than operator > ? 我如何在定义中使用成员函数(在这种情况下, magnitude() )来实现大于操作符>的函数重载? The operator should compare the magnitudes of two objects of the class Vector2D from which magnitude() is also a member. 操作员应比较Vector2D类的两个对象的大小,这些对象也属于Vector2D magnitude() I receive the following: 我收到以下信息:

error C2662: 'double Vector2D::magnitude(void)' : cannot convert 'this' >pointer from 'const Vector2D' to 'Vector2D &' 错误C2662:'double Vector2D :: magnitude(void)':无法将'this'>指针从'const Vector2D'转换为'Vector2D&'

 #include <iostream>
        #include <math.h>
        #include <string>
        using namespace std;

        //***************************  Class Definition  ***************************

        class Vector2D {
        public:
            double i, j;
            Vector2D();                     // Default constructor
            Vector2D(double, double);       // Constructor for initializing
            double  magnitude();            // compute and return the magnitude
            bool operator> (const Vector2D& right); // greater than                      
        };

        //function definitions:

        //default constructor
        Vector2D::Vector2D() {
            i = 1;
            j = 1;

        }
        /* constructor*/
        Vector2D::Vector2D(double x, double y) {
            i = x;
            j = y;
        }

        /* magnitude funciton */
        double Vector2D::magnitude()
        {
            return sqrt(pow(i, 2) + pow(j, 2));
        }

       ******* //greater than Overload ************

        bool Vector2D::operator> (const Vector2D& right) {
            if (magnitude() > right.magnitude())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    ***********************************************

Two changes: 两项更改:

  • bool Vector2D::operator> (const Vector2D& right) const
  • double Vector2D::magnitude() const

All read-only functions should be marked const . 所有只读功能应标记为const You may solve the error by just removing const from parameter of your > operator , but that would not be good. 您可以通过仅从> operator参数中删除const来解决错误,但这并不好。

The problem is that your functions aren't declared const , so can't be applied to constant objects. 问题在于您的函数未声明为const ,因此无法应用于常量对象。 That's easy to fix: 很容易解决:

double  magnitude() const;
bool operator> (const Vector2D& right) const;

If efficiency is important, you might like to avoid the unnecessary square-root when comparing magnitudes: 如果效率很重要,则在比较幅度时,您可能希望避免不必要的平方根:

double square_magnitude() const {return i*i + j*j;}
bool operator> (const Vector2D& right) const {
    return square_magnitude() > right.square_magnitude();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM