一关于矩阵类和重载运算符的题,求帮助不知道哪里错了
程序代码:
#include <iostream> #define M 3 #define N 3 using namespace std; class Matrix { public: Matrix(){} Matrix(int n[M][N]) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { m[i][j] = n[i][j]; } } } ~Matrix(){} Matrix operator+ (Matrix &m1) { Matrix m2,m3; int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { m3.m[i][j] =m2. m[i][j] + m1.m[i][j]; } } return m3; } void show() { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << m[i][j] <<" "<< endl; } cout << endl; } } private: int m[M][N]; }; int main() { int i, j; int a[M][N], b[M][N],c[M][N]; cout << "a is : " << endl; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { cin >> a[i][j]; } } cout << "b is :" << endl; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { cin >> b[i][j]; } } Matrix m1(a), m2(b),m3(c); m3=m1.operator+(m2); m3.show(); system("pause"); }
程序可以运行,但是最后的赋值总是赋值不上,不知道是哪里出问题了,求助~~~