有关C++运算符重载的几个问题

问题遇到的现象和发生背景 C++中的运算符重载
问题相关代码,请勿粘贴截图
#include
#include
using namespace std;

class MyInt
{
    
    friend ostream& operator<<(ostream &cout,MyInt A);
    friend void test01();
    
    public:
        
        MyInt& operator++()
        {
            this->m_A++;
            this->m_B++;
            return *this;
        }
        
        MyInt operator++(int)
        {
            MyInt temp;
            temp.m_A=this->m_A;
            temp.m_B=this->m_B;
            this->m_A++;
            this->m_B++;
            return temp;
        }
        
    private:
        int m_A;
        int m_B;
};

ostream& operator<<(ostream &cout,MyInt A)
{
    cout<<"m_A = "<"m_B = "<return cout;
}

void test01()
{
    MyInt MI1;
    MI1.m_A=0;
    MI1.m_B=0;
    cout<<"MI1(0,0)的++MI1:"<0;
    MI2.m_B=0;
    cout<<"MI2(0,0)的MI2++:"<int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果及报错内容

上面的代码可以正确运行,但是将ostream& operator<<(ostream &cout,MyInt A)改为ostream& operator<<(ostream &cout,MyInt &A)之后就会报错,cout<<"MI2(0,0)的MI2++:"<

ostream& operator<<(ostream &cout,MyInt A)
改引用的话需要把它改成const MyInt& ,因为A++ ,返回的是临时变量不能用左值引用绑定他

你改的时候友元的地方也要改,得一致,在第八行


可以查看手册:c++-运算符重载 中的内容