c++递减运算符重载出现的问题

图上的未经处理的异常是怎么回事?之前有人说是<<那的第二个&要去掉,可是明显不行,那样cout会因为局部不能进行两次--.帮忙解答一下这里的问题到底是什么呢谢谢!

img

img

后置++改成下面的:

my operator --(int)
    {
        my temp ;
        temp.m_num = this->m_num;
        this->m_num--; //这里本类的要--
        return temp; //返回的是原来的值
    }

运行结果:

img

完整代码:

#include <iostream>
using namespace std;

class my 
{
    friend ostream& operator<<(ostream& cout, my myint);
public:
    my() {
        m_num = 0;
    }
    my& operator --()
    {
        m_num--;
        return *this;
    }
    my operator --(int)
    {
        my temp ;
        temp.m_num = this->m_num;
        this->m_num--; //这里本类的要--
        return temp; //返回的是原来的值
    }
private:
    int m_num;
};

ostream& operator<<(ostream& cout, my myint)
{
    cout << myint.m_num;
    return cout;
}

void test01()
{
    my myint;
    cout << --(--myint) << endl;
    cout << myint << endl;
}

void test02()
{
    my youint;
    cout << youint-- << endl;
    cout << youint << endl;
}

int main()
{
    //test01();
    test02();
    return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632