kaikaideMC 2015-04-12 05:40 采纳率: 0%
浏览 1932

C++类中包含数组,如何重载运算符,第一次进入,还没有c币,求大神帮忙了,感激不尽

这是头文件
#include
#ifndef MATRIX_H_
#define MATRIX_H_
class matrix
{
private:
double a[20][20];
int m,n;
public:
matrix();
matrix(const matrix&a);
~matrix();
matrix operator=(const matrix&a);
friend void chengfa(const matrix&a, double b,matrix&c);
friend matrix& operator*(const matrix&a,double b);
friend matrix& operator*(double b,const matrix&a);
friend matrix& operator+(const matrix&a, const matrix&b);
friend matrix& operator-(const matrix&a, const matrix&b);
friend matrix& operator*(const matrix&a, const matrix&b);
friend std::ostream & operator<<(std::ostream&os, matrix&a);
friend std::istream & operator>>(std::istream&is, matrix&a);
};
#endif
这是对应的函数
#include
#include"matrix.h"
using std::cin;
using std::cout;
matrix::matrix()
{
for (int i = 0; i <20; i++)
for (int j = 0; j <20; j++)
a[i][j] = 0;

}
matrix::matrix(const matrix&x)
{
m = x.m;
n = x.n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = x.a[i][i];
}
matrix::~matrix(){}
std::istream & operator>>(std::istream &is, matrix&a)
{
cout << "please input the row of a:\n";
is >> a.m;
cout << "please input the lie of a:\n";
is >> a.n;
cout << "please input the member of a:\n";
for (int i = 0; i < a.m; i++)
for (int j = 0; j < a.n; j++)
is >> a.a[i][j];
return is;
}
std::ostream & operator<<(std::ostream &os, matrix&a)
{
for (int i = 0; i < a.m; i++)
for (int j = 0; j <a.n; j++)
{
os << a.a[i][j] << " ";
if (j == a.n-1)
os << "\n";
}
return os;
}
matrix matrix::operator=(const matrix&x)
{
m = x.m;
n = x.n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = x.a[i][j];
return*this;
}
matrix& operator*(const matrix&a,double b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] * b;
return c;

}

matrix& operator*(double b, const matrix&a)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] * b;
return c;
}
matrix& operator+(const matrix&a, const matrix&b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j]+b.a[i][j];
return c;
}
matrix& operator-(const matrix&a, const matrix&b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] - b.a[i][j];
return c;

}
matrix& operator*(const matrix&a, const matrix&b)
{
matrix c;
c = a - a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
for (int k = 0; k < c.n; k++)
c.a[i][j] += a.a[i][k] * b.a[k][j];
return c;
}
下面是主函数:
#include
#include"matrix.h"
void main()
{
using namespace std;
matrix a,b;
cout << "请输入a;" << endl;
cin >> a;
cout << "a是:" << endl;
cout << a << endl;

b = a*1.5;
cout << "b是:" << endl;
cout << b << endl;
cout << "a*b是:" << endl;
cout << a * b<< endl;

}
输出如下:
请输入a;
please input the row of a:
3
please input the lie of a:
3
please input the member of a:
1 2 3
4 5 6
7 8 9
a是:
1 2 3
4 5 6
7 8 9

b是:
1.5 3 4.5
6 7.5 9
10.5 12 13.5

a*b是:
45 54 63
99 121.5 144
153 189 225

Press any key to continue . . .

可是把重载运算符返回值前面的&去掉,结果就完全错误了

如下:
请输入a;
please input the row of a:
3
please input the lie of a:
3
please input the member of a:
1 2 3
4 5 6
7 8 9
a是:
1 2 3
4 5 6
7 8 9

b是:
1.5 1.5 1.5
7.5 7.5 7.5
13.5 13.5 13.5

a*b是:
57 57 57
124.5 124.5 124.5
192 192 192

Press any key to continue . . .

  • 写回答

1条回答 默认 最新

  • kaikaideMC 2015-04-12 05:41
    关注

    上面有个没有显示出来。

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误