转换构造函数出现问题
c++吧
全部回复
仅看楼主
level 1
作业班子 楼主
#include "stdlib.h"#
include "stdio.h"#include "iostream"using namespace std;class Complex{ public: Complex(){real = 0;imag = 0;} Complex(double r){real = r;imag = 0;} Complex(double r,double i){real = r;imag = i;} friend Complex operator+(Complex &,Complex &);// friend ostream & operator<<(ostream &,Complex &); private: double real; double imag; };Complex operator+(Complex &c1,Complex &c2){ return(Complex(c1.real+c2.real,c1.imag+c2.imag)); }/*ostream & operator<<(ostream &output,Complex c){ output << "(" << c.real << "," << c.imag << "i)" << endl; return output; }*/int main(){ Complex c1(3.0,4.0),c2(5,-10),c3; c3 = c1+2.5; //此处为什么不能通过编译呢?书上是这么写的// cout << c3}
2007年05月04日 13点05分 1
level 7
因为不可能为Complex(2.5)生成一个引用——这个对象会很快消失:#include 
using namespace std;class Complex{     public:      Complex(double r = 0,double i = 0) : real(r), imag(i)      {}      friend Complex operator+(const Complex,const Complex);//      friend ostream & operator<<(ostream &,Complex &);     private:      double real;      double imag;             };Complex operator+(const Complex c1,const Complex c2){     return(Complex(c1.real+c2.real,c1.imag+c2.imag));       }/*ostream & operator<<(ostream &output,Complex c){     output << "(" << c.real << "," << c.imag << "i)" << endl;     return output;       }*/int main(){     Complex c1(3.0,4.0),c2(5,-10),c3;     c3 = c1+2.5;//     cout << c3}
2007年05月05日 00点05分 2
level 0
ls一语惊醒梦中人
2009年10月13日 15点10分 3
1