并没有报错,但是就是不能通过,执行不了,就是找不大搜bug在哪里,请各位大佬给我看看,最好在附上vs的详细调试方法!
程序清单 matrix_h
#ifndef MATRIX_H_
#define MATRIX_H_
class matrix
{
public:
matrix();
matrix(int l, int w, int *n);
matrix operator+(matrix &m1);
void putmatrix();//输出矩阵
~matrix();
private:
int *num;
int length;
int width;
};
#endif
程序清单 matrix_cpp
#include<iostream>
#include"matrix.h"
using namespace std;
matrix::matrix()//无参构造函数
{
num = new int[length*width];
num = { 0 };
}
matrix::matrix(int l, int w, int *n)//重载构造函数
{
int i;
length = l;
width = w;
num = new int[length*width];
int *p = num;
for (i = 0; i<length*width; i++)
{
*p++ = *n++;
}
}
matrix matrix::operator+(matrix &m1)
{
matrix m2;
for (int i = 0; i < length*width; i++)
{
m2.num[i] = num[i] + m1.num[i];
}
return m2;
}
void matrix::putmatrix()//输出矩阵
{
for (int i = 0; i<length*width; i++)
{
cout << num[i] << " ";
if ((i + 1)%width == 0)
cout << endl;
}
}
matrix::~matrix()
{
delete[]num;
}
程序清单 main_cpp
#include<iostream>
#include"matrix.h"
using namespace std;
int main()
{
int arry1[2][2] =
{
{ 1, 2 },
{ 4, -5 }
};
int arry2[2][2] =
{
{ 3,-1},
{ 0,9}
};
matrix mt1(2, 2, *arry1);
matrix mt2(2, 2, *arry2);
matrix mt3;
mt3 = mt1 + mt2;
cout << "this is the first matrix arry1[2][2]:"<< endl;
mt1.putmatrix();
cout << "this is the first matrix arry2[2][2]:" << endl;
mt2.putmatrix();
cout << "this is the first matrix arry3[2][2]=arry1[2][2]+arry2[2][2]:" << endl;
mt3.putmatrix();
getchar();
return 0;
}
matrix mt3;
mt3 = mt1 + mt2;这两行改为matrix mt3 = mt1 + mt2;
num = { 0 };这行去掉