我正在尝试编写一个模板类函数来对矩阵进行一些基本操作。我一直在为内存问题而苦苦挣扎(很多,几乎一周),因为似乎每次我调用我的构造函数类时它都会破坏对象。我还怀疑当我为 + 运算符定义重载函数时出了什么问题,最奇怪的是我的代码可以很好地处理 2x2 矩阵,但仅此而已!
如果有人知道哪个可能是问题所在,我将不胜感激! 谢谢。
代码如下:
#include<iostream>
#include<stdlib.h>
#include <stdexcept>
#include<cstring>
using namespace std;
template<class TypeData>
class Matrix{
private:
int Nrows , Ncols;
TypeData *Array_Matrix;
public:
/*Constructors---------*/
Matrix(){
Nrows = 0;
Ncols = 0;
Array_Matrix = nullptr;
}
/*Constructor with 2 parameters:*/
Matrix(int _Nrows , int _Ncols){
this -> Nrows = _Nrows;
this -> Ncols = _Ncols;
Array_Matrix = new TypeData [Nrows * Ncols];
for(int i = 0 ; i<Nrows * Ncols ; i++){Array_Matrix[i] = 0.0;}
}
/*Copy constructor:*/
Matrix ( const Matrix &_M){
this -> Nrows = _M.Nrows ;
this -> Ncols = _M.Ncols;
Array_Matrix = new TypeData [Nrows * Ncols];
for(int i = 0 ; i<Nrows * Ncols ; i++){*(Array_Matrix + i) = *(_M.Array_Matrix + i);}
}
void Set_Value(int row , int col , TypeData value){
*(Array_Matrix + Ncols*row + col) = value;}
/*---------------------------------------------------------------------------------------------------------------------*/
TypeData Get_Pos_Value(int row , int col){
if(row >= Nrows || col >= Ncols){throw std::invalid_argument( "Error!, index out of bounds!" );}
else{return *(Array_Matrix + Ncols*row + col) ;}}
TypeData *Get_Array(){ /*Method for returning a pointer to the "matrix"*/
return Array_Matrix;};
int Get_Nrows(){
return Nrows;}
int Get_Ncols(){
return Ncols;}
void Print_Matrix(){
for (int i = 0 ; i<Nrows ; i++){
cout<<endl;
for (int j = 0 ; j<Ncols ; j++){
cout<<*(Array_Matrix + Ncols*i + j)<<" ";}}cout<<endl;}
/*Operator overloading --------------------------------------------------------------------------------------------*/
Matrix operator = (const Matrix &Matrix_object){
delete [] Array_Matrix;
this -> Nrows = Matrix_object.Nrows;
this -> Ncols = Matrix_object.Ncols;
Array_Matrix = new TypeData [Nrows * Ncols];
for (int i = 0 ; i<Nrows*Ncols ; i++){Array_Matrix [i] = Matrix_object.Array_Matrix[i];}
return *this;
}
Matrix operator + (const Matrix &Matrix_object)const{
if(Matrix_object.Nrows != Nrows || Matrix_object.Ncols != Ncols)
{throw std::invalid_argument("Different siex matrices cant be added!" );}
Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
for(int i = 0 ; i<Nrows*Ncols; i++){
//Temp.Array_Matrix[i] = Array_Matrix[i] + Matrix_object.Array_Matrix[i];
*(Temp.Array_Matrix +i) = *(Array_Matrix +i) + *(Matrix_object.Array_Matrix + i);}
return Temp;}
Matrix operator - (const Matrix &Matrix_object)const{
if(Matrix_object.Nrows != Nrows || Matrix_object.Ncols != Ncols)
{throw std::invalid_argument("Different size matrices can be substracted!" );}
Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
for(int i = 0 ; i<Nrows*Ncols; i++){
//Temp.Array_Matrix[i] = Array_Matrix[i] - Matrix_object.Array_Matrix[i];
*(Temp.Array_Matrix +i) = *(Array_Matrix +i) - *(Matrix_object.Array_Matrix + i);}
return Temp;}
Matrix operator += (const TypeData num){
Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
for(int i = 0 ; i<Nrows*Ncols; i++){
Temp.Array_Matrix[i] = Array_Matrix[i] + num ;} return Temp;}
Matrix operator -= (const TypeData num){
Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
for(int i = 0 ; i<Nrows*Ncols; i++){
Temp.Array_Matrix[i] = Array_Matrix[i]- num ;} return Temp;}
~Matrix(){ delete[] Array_Matrix;}
};
主要
int main(){
int r1 , c1; cout<<"Matrix1: Enter Number of rows and columns "; cin>>r1>>c1;
Matrix <float> My_Matrix(r1 , c1);
for (int i = 0 ; i<My_Matrix.Get_Nrows() ; i++){
for (int j = 0 ; j<My_Matrix.Get_Ncols(); j++){
float value; cout<<"Type the value at: ["<<i<<"]["<<j<<"] position "; cin>>value;
My_Matrix.Set_Value(i , j , value);}}
int r2 , c2; cout<<"Matrix2: Enter Number of rows and columns "<<endl; cin>>r2>>c2;
Matrix <float> My_Matrix2(r2,c2);
for (int i = 0 ; i<My_Matrix2.Get_Nrows() ; i++){
for (int j = 0 ; j<My_Matrix2.Get_Ncols(); j++){
float value; cout<<"Type the value at: ["<<i<<"]["<<j<<"] position "; cin>>value;
My_Matrix2.Set_Value(i , j , value);}}
auto My_Matrix3 = My_Matrix + My_Matrix2;
cout<<"Add: "<<endl;
My_Matrix3.Print_Matrix();
cout<<endl;
cout<<"Testing substraction: "<<endl;
auto My_Matrix4 = My_Matrix - My_Matrix2;
cout<<"Substraction is: "<<endl;
My_Matrix4.Print_Matrix();
cout<<endl;
cout<<"Testing = operator: "<<endl;
My_Matrix4 = My_Matrix3;
cout<<"Substraction matriz is now: "<<endl;
My_Matrix4.Print_Matrix();
cout<<endl<<"as the Add matrix! "<<endl;
cout<<"Testing +=:"<<endl;
int num ; cout<<"Please enter an integer number: ";cin>>num;
(My_Matrix4 += num).Print_Matrix();
return 0;}
Matrix <TypeData> Temp(0,0);
好消息:您创建了一个
Matrix
总共有 0 行、0 列和 0 个值。
Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
坏消息:行数和列数被其他东西破坏了,然后:
for(int i = 0 ; i<Nrows*Ncols; i++){
//Temp.Array_Matrix[i] = Array_Matrix[i] + Matrix_object.Array_Matrix[i];
*(Temp.Array_Matrix +i) = *(Array_Matrix +i) + *(Matrix_object.Array_Matrix + i);}
return Temp;}
...代码在
Temp.Array_Matrix
将具有 Nrows*Ncols
值的假设下进行。当然,事实并非如此。因此内存损坏。
相同的错误在显示的代码中多次出现。