C++自定义矩阵并重载“+”运算符,指针返回错误

问题:

重载“+”运算符,得到结果中指针保存的值错误。


解决过程:

原因:没有重载“=”运算符,而图中所示的方法调用了“=”运算符,默认的“=”运算符,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;


}

代码二:数据部分使用std::vector保存,就可以不重载“=”运算符了:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<iostream> 
#include<iomanip> 
#include <vector>;
using namespace std;

class Matrix
{
public:
	Matrix()
	{
		row = 0;
		column = 0;
		pt.resize(row*column);
	}
	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;
	vector<double> pt;
};
// 定义重载运算符   +
Matrix Matrix::operator+(Matrix & m)
{
	Matrix temp(m.row, m.column);
	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.at(i*column + j) = pt.at(i*column + j) + m.pt.at(i*column + 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)
{
	pt.resize(row*column);
	if (row < 1 || column < 1)   cout << "矩阵的行数和列数都必须大于0!" << endl;
	else
	{
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < column; j++)
			{
				pt.at(i*column + j) = 0.0;
			}
		}
	}
}
//拷贝构造函数,以矩阵A创建新矩阵B并以A对B进行初始化 
Matrix::Matrix(const Matrix &m)
{
	row = m.row;
	column = m.column;
	pt.resize(row*column);
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			pt.at(i*column + j) = m.pt.at(i*column + j);
		}
	}

}
//析构函数,对堆中的动态空间进行释放
Matrix::~Matrix()
{
	if (!pt.empty())
	{
		pt.clear();
	}
}
//矩阵赋值函数,对已创建的矩阵以人工输入的方式进行赋值
void Matrix::Creat()
{
	cout << "请输入" << row*column << "个数作为矩阵元素:" << endl;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			cin >> pt.at(i*column + 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.at(i*column + 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(r,c);
 	M3= M1 + M2;
	M3.Output();
	
	cin >> r >> c;
	return 0;


}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值