简体   繁体   English

C++:使用成员 function 的二元运算符重载

[英]C++: Binary operator overloading using member function

I've searched stackoverflow a bit and found some topics similar to what I am doing.我搜索了一下stackoverflow,发现了一些与我正在做的类似的主题。 However, I just do not understand yet the probably very simple errors I made.但是,我只是不明白我犯的可能非常简单的错误。

Can you please help me out?你能帮帮我吗? As you can see I make really basic mistakes.正如你所看到的,我犯了非常基本的错误。

The function is for practice purposes only. function 仅用于练习目的。 I want to have a class for points with x , y coordinates in double data type, these are privates.我想要一个 class 用于双数据类型的xy坐标点,这些是私有的。

To practice operator overloading/member function based operator access to privates, I wrote the following code.为了练习运算符重载/成员 function 基于运算符访问私有,我编写了以下代码。

In the end, I just want to print out the results of the addition of each of the coordinates x and y of two points.最后,我只想打印出两个点的坐标xy相加的结果。

What are the mistakes that I am making?我犯了什么错误?

Thank you in advance for any hints.提前感谢您的任何提示。

#include <stdio.h>
#include <iostream>

class point{
    
    public:
        // this operator adds a point "p2" to the point of class "point"
        // OVERLOADING BINARY + USING A MEMBER FUNCTION:
        point operator+(point p){ // n is the 2nd number in the addition
            return point(x+p.x, y+p.y); // equals (this->x+p2.x, ...)
        }
        
    // private numbers 
    private:
        //declaration and initialization of x and y:
        double x=1.1,y=2.2;
};

int main()
{
    // Create two new points:
    point *p1 = new point ();
    point *p2 = new point ();
    
    cout << "The result of the addition is: " << (*p1+*p2);
    
    return 0;
}

Currently the error I get is:目前我得到的错误是:

In member function ‘point point::operator+(point)’: main.cpp:16:38:
error: no matching function for call to ‘point::point(double, double)’
return point(x+p.x, y+p.y); // equals (this->x+p2.x, ...)

Like churill pointed out, you missed implementing two things:就像 churill 指出的那样,您错过了两件事:

First you need to implement a constructor for your point class that takes 2 doubles:首先,您需要为您的点 class 实现一个构造函数,该构造函数需要 2 个双精度:

point(double x, double y)
  : x(x), y(y)
{}

The above constructor is using initializer lists and does the same as this:上面的构造函数使用了初始化列表并且和下面的一样:

point(double x, double y) {
    this.x = x;
    this.y = y;
}

and don't forget to implement the default constructor again since otherwise C++ will not find it anymore and your code will fail to compile并且不要忘记再次实现默认构造函数,否则 C++ 将找不到它并且您的代码将无法编译

point() {}

Second You need to overwrite the "<<" operator which will allow you to cout your point object:其次,您需要覆盖“<<”运算符,这将允许您计算您的观点 object:

friend ostream& operator<<(ostream& os, const point& p) {
    os << "(" << p.x << ',' << p.y << ')'; // will output (x,y)
    return os;
}

if you put all the above in your point class as public it should compile without a problem如果您将以上所有内容都放在您的观点 class 中,它应该可以毫无问题地编译

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

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