关于C++转换构造函数的问题求教!
//本人用的是VS2010,编写了这个程序实现运算符的重载,中间用到了转换构造函数,但是程序一直编译不通过,请问是我的代码问题还是VS2010的问题吗?#include<iostream>
using namespace std;
class Complex
{
public:
Complex();
Complex(int,int);
~Complex();
Complex(double r){real=r;imag=0;} //此处定义转换构造函数
friend Complex operator+(Complex &a,Complex &b);
friend Complex operator-(Complex &a,Complex &b);
friend Complex operator*(Complex &a,Complex &b);
friend Complex operator/(Complex &a,Complex &b);
friend ostream&operator<<(ostream&output,Complex&exp);
friend istream&operator>>(istream&input,Complex&exp);
private:
double real;
double imag;
};
Complex::Complex(){}
Complex::Complex(int a,int b):real(a),imag(b){}
Complex::~Complex(){}
Complex operator+(Complex &a,Complex &b)
{
return Complex(a.real+b.real,a.imag+b.imag);
}
Complex operator-(Complex &a,Complex &b)
{
return Complex(a.real-b.real,a.imag-b.imag);
}
Complex operator*(Complex &a,Complex &b)
{
return Complex((a.real*b.real-a.imag*b.imag),(a.real*b.imag+a.imag*b.real));
}
Complex operator/(Complex &a,Complex &b)
{
return Complex((a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag),(a.imag*b.real-a.real*b.imag)/(b.real*b.real+b.imag*b.imag));
}
ostream&operator<<(ostream&output,Complex&exp)
{
if(exp.imag<0)
output<<exp.real<<"+"<<-exp.imag<<"i";
else
output<<exp.real<<"+"<<exp.imag<<"i";
return output;
}
istream&operator>>(istream&input,Complex&exp)
{
input>>exp.real>>exp.imag;
return input;
}
int main()
{
Complex a,b,c;
double d=9;
cin>>a;
c=a+d; //此处想利用转换构造函数将d转换为Complex类型(9+0i)然后利用运算符重载进行计算,但一直出错!
cout<<c<<endl;
system("pause");
}