qq_36271518 2021-03-30 17:58 采纳率: 100%
浏览 29
已采纳

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

#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;
}
  • 写回答

3条回答 默认 最新

  • lemon-l 2021-03-30 18:15
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集