簡體   English   中英

在 C++ 中添加矩陣

[英]Addition of Matrix in C++

下面的代碼給了我錯誤的 output。實際上,它不是在求和,它實際上是將第 2 個 object 復制到 M3 object 而不是計算求和。 我認為我在 + 運算符重載方面存在一些邏輯錯誤。 有人有任何想法或任何其他建議嗎? 它顯示在復制構造函數 function cout<data[r][c]<<"\t"; 中實際調用的 output。 但是當我使用 M3.displayData() 時它沒有顯示 output。 #包括

#include <string.h>
using namespace std;

class Matrix{
    private:
        int noOfRows;
        int noOfColumns;
        int **data;
    public:
        Matrix(int noOfRows, int noOfColumns);
        void displayData();
        ~Matrix();
        Matrix (const Matrix &ref);
        Matrix operator + (Matrix m);
        Matrix& operator=(Matrix m) { 
        std::swap(m.noOfRows, noOfRows); 
        std::swap(m.noOfColumns, noOfColumns); 
        std::swap(m.data, data); 
        return *this; }
};

Matrix::Matrix(int inr=0, int inc=0){
    noOfRows=inr; noOfColumns=inc;
    data=new int*[noOfColumns];
    for(int i=0;i<noOfRows;i++)
        data[i]=new int[noOfColumns];
    int d;
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++){
            cout<<"Enter ...";cin>>d;
            data[r][c]=d;
        }
        cout<<endl;
    }
}

Matrix::Matrix (const Matrix &ref){
    this->data=new int*[ref.noOfColumns];
    for(int i=0;i<ref.noOfRows;i++)
        this->data[i]=new int[ref.noOfRows];
    
    for(int r=0;r<ref.noOfRows;r++){
        for(int c=0;c<ref.noOfColumns;c++){
            this->data[r][c]=ref.data[r][c];
            cout<<this->data[r][c]<<"\t";
        }
        cout<<endl;
    }
}

Matrix Matrix::operator + (Matrix m){
    Matrix ms(m.noOfRows,m.noOfColumns);
    ms=0;
    for (int i=0; i<m.noOfRows; i++) 
        for (int j=0; j<m.noOfColumns; j++){
        ms.data[i][j] = data[i][j]+m.data[i][j];
        return ms;
        } 
    }

void Matrix::displayData(){
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++)
            cout<<data[r][c]<<"\t";
        cout<<endl;
    }
}

Matrix::~Matrix(){
    delete[] data;
}

int main(){
    Matrix M1(2,2),M2(2,2);
    cout<<"\n Matrix A="<<endl;
    M1.displayData();
    cout<<"\n Matrix B="<<endl;
    M2.displayData();
    cout<<"\n Sum of Matrix="<<endl;
    Matrix M3=M1+M2;
    M3.displayData();
    return 0;
}

程序輸出在這里:

您的代碼至少有四個問題:

  1. Matrix復制構造函數無法復制noOfRowsnoOfColumns值。

  2. 您使用noOfColumns作為行數錯誤地分配了行指針。

  3. Matrix operator +中,您在for循環內返回Matrix ,而您應該在循環完成后返回它。

  4. 您的析構函數無法delete[]所有行和列數據。

要解決前兩個問題,因為Matrix默認構造函數和復制構造函數之間有很多公共代碼,您可以創建一個Allocate成員 function 來分配 memory:

class Matrix
{
    private:
       void Allocate();
    //... other members
    public:
        Matrix operator + (const Matrix& m);
    // other members...
};

Matrix::Matrix(int inr=0, int inc=0) : noOfRows(inr), noOfColumns(inc)
{
    Allocate();
    // Input code removed...
}

Matrix::Matrix (const Matrix &ref) : noOfRows(ref.noOfRows), noOfColumns(ref.noOfColumns)
{
    Allocate();
    for(int r=0; r < ref.noOfRows; r++)
    {
        for(int c=0; c < ref.noOfColumns; c++)
            data[r][c] = ref.data[r][c];
    }
}

void Matrix::Allocate()
{
    data=new int*[noOfRows];
    for(int i=0;i < noOfRows; i++)
        data[i]=new int[noOfColumns]();
}

對於operator + ,您應該通過 const 引用而不是值傳遞Matrix (這是為了修復過早返回Matrix的錯誤):

Matrix Matrix::operator + (const Matrix& m)
{
    Matrix ms(m.noOfRows,m.noOfColumns);
    for (int i=0; i<m.noOfRows; i++) 
    { 
        for (int j=0; j<m.noOfColumns; j++)
            ms.data[i][j] = data[i][j]+m.data[i][j];
    }
    return ms;
}

最后一個問題(析構函數)只移除行指針,但不會移除為每一行分配的數據。 修復如下:

Matrix::~Matrix()
{
    for (int i = 0; i < noOfRows; ++i)
        delete[] data[i];
    delete [] data;
}

其他事宜:

  1. 在運算符之間留出更多空間。 您當前的代碼將所有內容壓縮在一起,使其難以閱讀。 例如:

for(int c=0;c<ref.noOfColumns;c++)可以是: for (int c=0; c < ref.noOfColumns; c++)

  1. 過度和不必要地使用this->

  2. 如果Matrix::operator +存在,那么Matrix::operator +=也存在是有意義的。 對於后者, operator +將簡單地根據operator +=來實現,使operator +一兩行代碼。

  3. 這個答案說明了一種可能更好的分配 memory 的方法,因為只完成了兩次分配,分配的 memory 是連續的,並且會發生更少的 memory 碎片。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM