malloc():在c++中重载+运算符时顶部大小损坏

bwleehnv  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(143)

我正在尝试写一个模板类函数来做一些矩阵的基本运算。我一直在努力(很多,几乎一个星期)与内存问题,因为它似乎每次我调用我的构造函数类它破坏的对象。我也怀疑有什么是错误的,当我定义重载函数的+操作符,最奇怪的是我的代码可以很好地处理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;}
2skhul33

2skhul331#

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值的假设。当然,它没有。因此内存崩溃。
相同的错误在所示代码中出现多次。

相关问题