#include<iostream.h>// c++6.0不支持把成员函数重载为友元函数,,所以用带。h的库 class Complex { int real; int imag; public: Complex(int a,int b):real(a),imag(b){} Complex(){}; Complex(int a){real=a;imag=0;} void display(); friend Complex operator +(Complex &c1,Complex &c2); }; Complex operator+(Complex&c1,Complex&c2) { return Complex(c1.real+c2.real,c1.imag+c2.imag);//注意返回带Cmplex } void Complex::display() { cout<<real<<'+'<<imag<<'i'<<endl; } int main() { Complex c1(1,5),c2(3,7),c3;int a=2; c3=c1+a; cout<<"c3="<<endl; c3.display(); return 0; } 我已经定义了转换构造函数,为什么c1+a会出错 为什么在complex&c1,Complex&c2)中加const就没问题啦求大神求解决,谢谢啦! {
莫回无
慕运维8079593
临摹微笑
相关分类