operator+中的为什么不写构造函数是错的?

class person
{
public:
    person(){}
    person(int a,int b):m_a(a),m_b(b){
    }
    person operator+(person &t){
        person temp;
        temp.m_a = this->m_a + t.m_a;
        temp.m_b = this->m_b + t.m_b;
        return temp;
    }

public:

    int m_a;
    int m_b;
};

为什么没写person(){}是错的?

因为你重载+的时候建立的对象temp用到了默认构造函数来初始化数据成员,且由于你定义了一个带参数的构造函数,编译器不会自动生成默认构造函数。

所以需要显式声明一个默认构造函数。