为了账号安全,请及时绑定邮箱和手机立即绑定

c++改错怎么改?

c++改错怎么改?

C++
灬elliott 2016-07-02 16:30:35
#include <iostream.h>#include "stdlib.h"class CComplex{public:CComplex(double r = 0, double i = 0){  real = r;  imag = i;}int operator int(){  return (int)real;}void Display(void){  cout << "(" << real << "," << imag << ")" << endl;}protected:double real;double imag;};class CVector{public:CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4){  objArray[0] = obj1;  objArray[1] = obj2;  objArray[2] = obj3;  objArray[3] = obj4;}friend CComplex &operator[](CVector obj, int n);private:CComplex objArray[4];};CComplex &operator[](CVector obj, int n){if(n<0 || n>3){  cout<<"Out of range!"<<endl;  exit(0);}return obj.objArray[n];}void main(){CComplex c1(1.1, 1.1);CComplex c2(2.2, 2.2);CComplex c3(3.3, 3.3);CComplex c4(4.4, 4.4);CVector v(c1,c2,c3,c4);v[0].Display();v[1].Display();v[2].Display();v[3].Display();v[0] = 5.5; ----------------------------------------------------------①v[1] = CComplex(6.6); -------------------------------------------②v[2] = int(CComplex(7.7)); --------------------------------------③v[3] = int(CComplex(8.8,9.9)); ----------------------------------④v[0].Display();v[1].Display();v[2].Display();v[3].Display();}
查看完整描述

1 回答

已采纳
?
onemoo

TA贡献883条经验 获得超454个赞

  1. CComplex 中的转型函数 operator int() 前面不用写返回类型

  2. 重载下标操作符 operator[] 必须是成员函数。 当然其参数只需要一个int就可以了。

  3. main函数的返回类型是int,不要写成void。


查看完整回答
1 反对 回复 2016-07-03
  • 灬elliott
    灬elliott
    1,2是显示转换还是隐式转换,为什么,3.4的转换过程是啥
  • onemoo
    onemoo
    因为你没有定义赋值函数,编译器自动生成的赋值函数要求等号右侧操作数也是CComplex类型。 1.等号右侧是浮点型5.5,会隐式转换为CComplex类型(5.5,0.0)。 2.等号右侧显式构造了一个CComplex。 3和4.先是显式构造了一个CComplex。然后按照你定义的转型函数,显式转型为int。随后再隐式转换为CComplex。
  • 灬elliott
    灬elliott
    谢谢啦
  • 1 回答
  • 0 关注
  • 2122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信