|
发表于 2020-3-29 14:41:38
|
显示全部楼层
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex()
- {
- real=0;
- imag=0;
- }
- Complex(double t,double d)
- {
- real=t;
- imag=d;
- }
-
- Complex operator +(int &x); /*不加const显示编译错误*/
-
- void display()
- {
- cout<<"("<<real<<","<<imag<<"!)"<<endl;
- }
- private:
- double real;
- double imag;
- };
- Complex Complex::operator +(int &x) /*不加const显示编译错误*/
- {
- return Complex(real+x,imag);
- }
- int main()
- {
- Complex t1(3.4,5.2);
- Complex t3,t4;
- //t3=t1+2; // 是这里导致的,加const就是为了这里
- int a = 1234;
- t3 = t1 + a;
- t3.display();
- return 0;
- }
复制代码 |
|