主题:求助
编了一个用类解复数加法的程序,是这样的:
#include<iostream.h>
class complex
{
int real;
int im;
public:
complex(int m,int n)
{
real=m;
im=n;
}
complex operator+(complex c1);
complex operator=(complex c1);
void show();
};
complex complex::operator+(complex c1)
{
complex c3;
c3.real=real+c1.real;
c3.im=im+c1.im;
return c3;
}
complex complex::operator=(complex c1)
{
real=c1.real;
im=c1.im;
return *this;
}
void complex::show()
{
cout<<real<<"+("<<im<<"i)"<<endl;
}
main()
{
complex a(2,5),b(7,8),c(0,0);
c=a+b;
c.show();
return 0;
}
但是出现了下面这个错误
C:\Cpp1.cpp(19) : error C2512: 'complex' : no appropriate default constructor available:
但是当我把“complex complex::operator+(complex c1)”中的C3初始化后就没错了,为什么会这样呢?
#include<iostream.h>
class complex
{
int real;
int im;
public:
complex(int m,int n)
{
real=m;
im=n;
}
complex operator+(complex c1);
complex operator=(complex c1);
void show();
};
complex complex::operator+(complex c1)
{
complex c3;
c3.real=real+c1.real;
c3.im=im+c1.im;
return c3;
}
complex complex::operator=(complex c1)
{
real=c1.real;
im=c1.im;
return *this;
}
void complex::show()
{
cout<<real<<"+("<<im<<"i)"<<endl;
}
main()
{
complex a(2,5),b(7,8),c(0,0);
c=a+b;
c.show();
return 0;
}
但是出现了下面这个错误
C:\Cpp1.cpp(19) : error C2512: 'complex' : no appropriate default constructor available:
但是当我把“complex complex::operator+(complex c1)”中的C3初始化后就没错了,为什么会这样呢?