fighter_potter 2016-05-10 02:56 采纳率: 0%
浏览 1095

一个简单的输入运算符重载

#include
#include
using namespace std;
class complex{
private:
int real;
int imag;
public:
complex(int r=0,int i=0) :real(i),imag(i){}
~complex(){}
complex operator +(complex&x)
{
real=real+x.real;
imag=imag+x.real;
return *this;
}
complex operator -(complex&x)
{
real=real-x.real;
imag=imag-x.imag;
return *this;
}
complex operator ++() //前置自增不允许有参数,作为类成员
{
real++;
return *this;
}
complex operator ++(int)
{
imag++;
return *this;
}
complex operator =(complex&x)
{
real=x.real;
imag=x.imag;
return *this;
}

void output()
{
    cout<<real<<" "<<imag<<endl;
}
friend istream operator >>(istream&in,complex&x);

};
istream operator >>(istream&in,complex&x)
{
in>>x.real>>x.imag;
return in;
}
int main()
{
complex c(1,1);
complex d(10,10);
complex e;
e=c+d;
e.output();
e=c-d;
e.output();
c++;
c.output();
++c;
c.output();
return 0;
}

**这段代码,报错说重载的函数 return in是尝试引用被删除的函数、这什么鬼?=-=菜鸟一枚,求解答**
  • 写回答

1条回答

  • 小灸舞 2016-05-10 03:20
    关注

    对>>的重载返回值类型改成引用类型就行了

     #include <iostream>
    using namespace std;
    using namespace std;
    class complex{
    private:
        int real;
        int imag;
    public:
        complex(int r=0,int i=0) :real(i),imag(i){}
        ~complex(){}
        complex operator +(complex&x)
        {
            real=real+x.real;
            imag=imag+x.real;
            return *this;
        }
        complex operator -(complex&x)
        {
            real=real-x.real;
            imag=imag-x.imag;
            return *this;
        }
        complex operator ++() //前置自增不允许有参数,作为类成员
        {
            real++;
            return *this;
        }
        complex operator ++(int)
        {
            imag++;
            return *this;
        }
        complex operator =(complex&x)
        {
            real=x.real;
            imag=x.imag;
            return *this;
        }
        void output()
        {
            cout<<real<<" "<<imag<<endl;
        }
        friend istream &operator>>(istream&in,complex&x);
    };
    istream &operator>>(istream&in,complex&x)
    {
        in>>x.real>>x.imag;
        return in;
    }
    int main()
    {
        complex c(1,1);
        complex d(10,10);
        complex e;
        e=c+d;
        e.output();
        e=c-d;
        e.output();
        c++;
        c.output();
        ++c;
        c.output();
        return 0;
    }
    

    第一个形参是一个引用,因为不能复制istream对象(在c++中定义的标准输入输出流类istream和ostream,其中拷贝构造函数和赋值操作符函数都被放置在了private部分,且只有声明,没有定义)。
    首先因为istream对象不能复制,所以必须是引用;其次引用可以少一次拷贝,提高效率;最后,为了体现连续性,实现连续输入,达到用多个输入操作符操作一个istream对象的效果,如果不是引用,程序返回的时候就会生成新的临时对象

    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)