husky66
husky66
采纳率72.2%
2020-05-09 11:13

自增运算符重载问题?求解

已采纳
#include<iostream>

using namespace std;

class Person
{
    friend ostream & operator<<(ostream &cout, Person &P);
public:
    Person(int A)
    {
        m_A = A;
    }

//自增运算符重载
    Person operator++(int)
    {
        Person P1 = *this;
        m_A++;
        return P1;
    }
private:
    int m_A;
};

//左移运算符重载
ostream & operator<<(ostream &cout, Person &P)
{
    cout << P.m_A;
    return cout;
}

void text()
{
    Person P(10);
    cout << P++ << endl;
    cout << P << endl;

}

int main()
{
    text();

    system("pause");
    return 0;
}

为什么 cout << P++ << endl这段代码出错了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 1年前

    问题解决的话,请点采纳

        Person& operator++(int)
        {
            Person& P1 = *this;
            m_A++;
            return P1;
        }
    
    点赞 评论 复制链接分享