成员运算符重载函数,把注释的代码还原回来编译就通不过,为什么,如何解决?

#include <iostream>
#include <cmath>

using namespace std;

class Point;

class Manager
{
public:
    void Myprint(const Point & another);
    //Point operator +(Point & a,Point & b);
};


class Point
{
    friend void Manager::Myprint(const Point & another);
    //friend Point Manager::operator +(Point & a,Point & b);
public:
    Point(int a=1,int b=2)
        :m_x(a),m_y(b)
    {

    }

    void func()
    {
        cout << "(" << this->m_x << "," << this->m_y << ")" << endl;
    }

private:
    int m_x;
    int m_y;
};



void Manager::Myprint(const Point & another)
{
    cout << "Myprint():(" << another.m_x << "," << another.m_y << ")" << endl;
}

//Point Manager::operator +(Point & a,Point & b)
//{
//    a.m_x += b.m_x;
//    a.m_y += b.m_y;
//    return a;
//}




int main()
{
   Point p1(3,4);
   Manager m;
   m.Myprint(p1);

    return 0;
}

 

+号运算符的操作数为2,你在Manager里面重载的这个实际上有3个参数,有一个隐形的参数是this,操作数为3,所以会有问题。你要么在Point内部重载只有一个参数的成员函数,要么重载一个两个参数的非成员函数

因该是大写分号吧!

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,欢迎您加入CSDN!

目前问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632