注册 登录
编程论坛 C++教室

error: no matching function for call to 'Complex::Complex()'|

床至只局 发布于 2015-04-26 21:50, 940 次点击
#include<iostream>
using namespace std;

class Complex
{
private:
    int real,imag;
protected:
public:
    Complex(int r,int i);
    ~Complex();
    void show();
    friend ostream &operator<<(ostream &os,const Complex &f);
    friend istream &operator>>(istream &is,Complex &f);
    Complex operator+(Complex &obj2);
    Complex operator-(Complex &obj2);
    Complex operator*(Complex &obj2);
};
Complex Complex::operator+(Complex &obj2)
{
    Complex temp;//这里有错???????????????????????????????????????????????
    temp.real=this->real+obj2.real;
    temp.imag=this->imag+obj2.imag;
    return temp;
}
Complex Complex::operator-(Complex &obj2)
{
    this->real-=obj2.real;
    this->imag-=obj2.imag;
    return *this;
}
Complex Complex::operator*(Complex &obj2)
{
    this->real*=obj2.real;
    this->imag*=obj2.imag;
    return *this;
}
Complex::Complex(int r=0,int i=0):real(r),imag(i)
{
    cout<<real<<"+"<<imag<<"i"<<endl;
    cout<<"storage space allocation."<<endl;
}
Complex::~Complex()
{
    cout<<"delete space finished!"<<endl;
}
void Complex::show()
{
    cout<<real<<"+"<<imag<<"i"<<endl;
}
ostream &operator<<(ostream &os,const Complex &f)
{
    os<<f.real<<"+"<<f.imag<<"i"<<endl;
}
istream &operator>>(istream &is,Complex &f)
{
    is>>f.real>>f.imag;
    return is;
}

int main()
{
    Complex obj,obj2;
    cin>>obj>>obj2;
    cout<<obj<<obj2;
    cout<<obj+obj2<<endl<<obj-obj2<<endl<<obj*obj2;
    return 0;
}
2 回复
#2
yangfrancis2015-04-26 22:30
也许是因为你没有定义不带参的构造函数吧,再重载一个构造函数看能不能通过。我猜的。
#3
床至只局2015-04-26 22:59
回复 2楼 yangfrancis
谢谢你,按照你的建议我重新修改了一下,程序顺利运行了
1