重载“+”运算符,得到结果中指针保存的值错误。
原因:没有重载“=”运算符,而图中所示的方法调用了“=”运算符,默认的“=”运算符,int变量成功返回值,但是指针只能返回野指针。
Matrix& operator=(/*const*/ Matrix & m);注意:要实现重载,还需要定义拷贝构造函数:
Matrix(const Matrix &); //拷贝构造函数补充:后来发现,如果不调用“=”运算符,即使重载“=”也没有关系,即按照下面的形式使用:
(M1 + M2).Output()以Matrix为返回值的函数,在返回时会调用拷贝构造函数。由于自己定义矩阵的数值保存使用了指针,如果直接调用“=”运算符会导致得到的值错误,所以在使用“=”的话,一定要自己定义“=”运算符来拷贝指针的内容。
直接上代码吧,已经正常运行了
其中,“=”运算符中的“const”符号可以不加。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include<iomanip> using namespace std; class Matrix { public: Matrix() { row = 0; column = 0; } Matrix(int, int); //构造函数 Matrix(const Matrix &); //拷贝构造函数 ~Matrix(); //析构函数 void Creat(); //矩阵赋值函数 void Output(); //矩阵输出函数 Matrix operator+(Matrix & m2); //声明重载矩阵运算符 + Matrix& operator=(const Matrix & m); private: int row; int column; double **pt; }; // 定义重载运算符 + Matrix Matrix::operator+(Matrix & m) { Matrix temp(m); if (row != m.row || column != m.column) cout << "矩阵M1和矩阵M2不是同类型矩阵,不能相加!" << endl << endl; else { temp.row = row; temp.column = column; //temp.pt = new double*[temp.row]; for (int i = 0; i < temp.row; i++){ //temp.pt[i] = new double[temp.column]; for (int j = 0; j < temp.column; j++) { temp.pt[i][j] = pt[i][j] + m.pt[i][j]; } } } return temp; } Matrix& Matrix::operator=(const Matrix & x) { if (pt) { for (int i = 0; i < row; ++i) delete[] pt[i]; delete[] pt; } row = x.row; column = x.column; pt = new double *[row]; for (int i = 0; i < row; ++i) pt[i] = new double[column]; for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) pt[i][j] = x.pt[i][j]; return *this; } //创建row行column列的矩阵,并将其初始化为零阵 Matrix::Matrix(int r, int c) :row(r), column(c) { if (row < 1 || column < 1) cout << "矩阵的行数和列数都必须大于0!" << endl; else { pt = new double*[row]; for (int i = 0; i < row; i++) { *(pt + i) = new double[column]; for (int j = 0; j < column; j++) { *(*(pt + i) + j) = 0.0; } } } } //拷贝构造函数,以矩阵A创建新矩阵B并以A对B进行初始化 Matrix::Matrix(const Matrix &m) { row = m.row; column = m.column; pt = new double*[row]; for (int i = 0; i < row; ++i) { pt[i] = new double[column]; } for (int i = 0; i < row; ++i) { for (int j = 0; j < column; ++j) { pt[i][j] = m.pt[i][j]; } } } //析构函数,对堆中的动态空间进行释放 Matrix::~Matrix() { if (pt != NULL) { for (int i = 0; i < row; i++) delete[] pt[i]; delete[] pt; } } //矩阵赋值函数,对已创建的矩阵以人工输入的方式进行赋值 void Matrix::Creat() { cout << "请输入" << row*column << "个数作为矩阵元素:" << endl; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { cin >> *(*(pt + i) + j); } } cout << "===============================================================================" << endl; } //矩阵输出函数,以矩阵的形式将矩阵输出 void Matrix::Output() { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { cout << left << setw(5) << *(*(pt + i) + j); } cout << endl; } } int _tmain(int argc, _TCHAR* argv[]) { system("COLOR 0e"); int r, c; cout << "请输入矩阵A的行数(N)和列数(M):" << endl; cin >> r >> c; Matrix M1(r, c); M1.Creat(); cout << "请输入矩阵B的行数(N)和列数(M):" << endl; cin >> r >> c; Matrix M2(r, c); M2.Creat(); Matrix M3; M3= M1 + M2; M3.Output(); // cin >> r >> c; // getchar(); return 0; }