Jacky wa 2018-12-14 12:31 采纳率: 0%
浏览 1708

如何在C++中用重载让两个数组相加?

这是我写的代码,没有错误但是没有结果。请大佬给我看一下,谢谢!
#include
#include
#include
using namespace std;
class Myclass
{
public:
int x = 0, y = 0;
Myclass() {}
void array()
{
int i = 0, j = 0;
do
{
cin >> a[i][j]; j++; y = j;
if (j % 3 == 0) { i++; x = i; }
} while (a[i][j] != EOF);
}
Myclass(int i, int j)
{
srand(int(time(0)));
for (int o = 0; o < i; o++)
{
for (int p = 0; p < j; p++)
b[o][p] = rand() % +100;
}
}
Myclass operator+(Myclass&none)
{
Myclass in;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
in.c[i][j] = a[i][j] + b[i][j];
}
}
return in;
}
void show()
{
for (int p = 0; p < x; p++)
{
for (int o = 0; o < y; o++)
{
cout << c[p][o];
}
cout << endl;
}
}
private:
int a[20][20], b[20][20], c[20][20];
};
int main()
{
Myclass one;
cout << "please input one array:" << endl;
one.array(); Myclass two(one.x, one.y);
Myclass three; three = one + two; three.show();
return 0;
}

  • 写回答

1条回答 默认 最新

  • _ZGq 2018-12-14 12:48
    关注

    array函数中把do-while循环改成while循环,并且把循环条件改成cin>>a[i][j],把循环体中的cin语句去掉。因为cin不能读入EOF。

    现在看来应该是x和y有问题(我让程序输出了one.x和one.y):
    please input one array:
    1 2 3 4 5 6 7 8 9 10 11 -12 -13 20 21 t //以非整数为读入数据的结束
    (69,208)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog