猿问

c++改错怎么改?

#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();
}


灬elliott
浏览 2189回答 1
1回答

onemoo

CComplex 中的转型函数 operator int() 前面不用写返回类型。重载下标操作符 operator[] 必须是成员函数。 当然其参数只需要一个int就可以了。main函数的返回类型是int,不要写成void。
随时随地看视频慕课网APP
我要回答