关于重载[]的问题,帮帮我

weixin_45067553 2019-05-22 04:00:27
这个人源程序,帮我看一下哪错了谢谢 #include <iostream> #include "stdlib.h" using namespace std; class CComplex { public: CComplex(double r = 0, double i = 0) { real = r; imag = i; } 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; } CVector &operator [](int n); private: CComplex objArray[4]; }; CComplex &CVector::&operator[](int n); }; void CVector::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(); }
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_45067553 2019-05-23
  • 打赏
  • 举报
回复
引用 2 楼 Italink的回复:
你是想这样写吧

#include <iostream>
#include "stdlib.h"
using namespace std;
class CComplex
{
public:
	CComplex(double r = 0, double i = 0)
	{
		real = r;
		imag = i;
	}
	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;
	}
	CComplex& operator [](int n);
private:
	CComplex objArray[4];
};
CComplex& CVector::operator[](int n)
 {
	 if (n < 0 || n>3)
	 {
		 cout << "Out of range!" << endl;
		 exit(0);
	 }
	 return 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();
 }
真厉害👍大佬。。。。
weixin_45067553 2019-05-23
  • 打赏
  • 举报
回复
引用 2 楼 Italink的回复:
你是想这样写吧

#include <iostream>
#include "stdlib.h"
using namespace std;
class CComplex
{
public:
	CComplex(double r = 0, double i = 0)
	{
		real = r;
		imag = i;
	}
	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;
	}
	CComplex& operator [](int n);
private:
	CComplex objArray[4];
};
CComplex& CVector::operator[](int n)
 {
	 if (n < 0 || n>3)
	 {
		 cout << "Out of range!" << endl;
		 exit(0);
	 }
	 return 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();
 }
真厉害👍大佬。。。。
Italink 2019-05-22
  • 打赏
  • 举报
回复
你是想这样写吧

#include <iostream>
#include "stdlib.h"
using namespace std;
class CComplex
{
public:
	CComplex(double r = 0, double i = 0)
	{
		real = r;
		imag = i;
	}
	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;
	}
	CComplex& operator [](int n);
private:
	CComplex objArray[4];
};
CComplex& CVector::operator[](int n)
 {
	 if (n < 0 || n>3)
	 {
		 cout << "Out of range!" << endl;
		 exit(0);
	 }
	 return 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();
 }
czopg 2019-05-22
  • 打赏
  • 举报
回复
重载函数返回值类型应该是int而不是void吧

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧