找不到错误在哪里,第一次碰到这个错误,求解


#include
using namespace std;

class complex
{
public:
    complex();
    complex(double r,double i):real(r),imag(i){}
    void display();
    complex operator+(complex&);
    friend ostream & operator<<(ostream&, complex&);
private:
    double real;
    double imag;
};

complex complex::operator+(complex& b)
{return complex(real + b.real, imag + b.imag);}

ostream& operator<<(ostream& output, complex& a)
{
    if(a.imag>=0)
        output << "(" << a.real << "+" << a.imag << "i)" << endl;
    else
        output << "(" << a.real << a.imag << "i)" << endl;
    return output;
}


int main()
{
    complex com1(3, 5), com2(7,-2),com3;
    com3 =com1 + com2;
    cout << com3<return 0;
}


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1120 1 个无法解析的外部命令 单目运算符重载 1
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 "public: __cdecl complex::complex(void)" (??0complex@@QEAA@XZ),函数 main 中引用了该符号 单目运算符重载 1

complex(); 默认构造函数没写函数体,改为 complex() {}

参考GPT和自己的思路:这个错误的原因可能是因为在代码中定义了一个无参构造函数,但是没有实现该函数的方法体,在main函数中使用了该构造函数,导致链接器无法解析该符号。解决方法是实现该构造函数的方法体,或者将该构造函数声明为私有的并且不使用它。同时,还要确保头文件和定义文件的包含顺序正确。