无法填充动态数组

Can't fill dynamic array

我现在正在学习C++,有一些误解 我创建了二维动态数组,但无法填充它 我在第 44 行中遇到错误,例如:访问冲突写入位置 0xFDFDFDFD。 这是我的代码:

#include <iostream>
#include <cmath>

using namespace std;

class Matrix
{
private:
    int row;
    int column;
    int** M;
public:
    Matrix();
    void setRow(int row) {
        this->row = row;
    }
    void setColumn(int column) {
        this->column = column;
    }
    int getRow() {
        return row;
    }
    int getColumn() {
        return column;
    }
    void setMat();
    void printMat();
    ~Matrix();
    
};

Matrix::Matrix() {
    M = new int* [row];
    for (int i = 0; i < row; i++) {
        M[i] = new int[column];
    }
    cout << "Constructor called";
}
//filling matrix
void Matrix::setMat() {
    for (int i = 0; i < row; i++) {
        cout << "Enter the first row";
        for (int j = 0; j < column; j++) {
            M[i][j]=j;
        }
    }
}
void Matrix::printMat() {
    for (int i = 0; i < row; i++) {
        cout << endl;
        for (int j = 0; j < column; j++) {
            cout<< M[i][j];
        }
    }
}
Matrix::~Matrix() {
    for (int i = 0; i < row; i++) {
        delete[] M[i];
    }
    delete[] M;
    cout << "constructor cancelled";
}
int main(){
    int numC, numR;
    Matrix a;
    cout << "Enter the number of column: ";
    cin>>numC;
    a.setColumn(numC);
    cout << "Enter the number of rows: ";
    cin >> numR;
    a.setRow(numR);
    int col = a.getRow();
    cout << col;
    a.setMat();
    a.printMat();
    
    
}

我认为问题出在动态数组上,但我不知道如何解决。我希望你能帮助我。还有关于列和行,我知道矢量,但任务是创建行和列。

这个声明

Matrix a;

在使用默认构造函数的地方,数据成员 rowcolumn 未初始化。因此至少构造函数会调用未定义的行为,其中未初始化的数据成员用于动态分配内存。

Matrix::Matrix() {
    M = new int* [row];
    for (int i = 0; i < row; i++) {
        M[i] = new int[column];
    }
    cout << "Constructor called";
}.

您需要为数据成员行和列使用一些默认值,或者使用两个参数重新声明构造函数,这两个参数将为数据成员设置值。

另外成员函数setRowsetColumn使得对象的状态不一致,因为对象动态分配的内存没有根据数据成员的新值重新分配rowcolumn.

您的构造函数正在 rowcolumn 成员初始化之前分配数组。您需要 re-think 您的设计。

尝试更像这样的东西:

#include <iostream>
#include <cmath>

using namespace std;

class Matrix
{
private:
    int row;
    int column;
    int** M;

public:
    Matrix(int row, int column);
    ~Matrix();

    int getRow() const {
        return row;
    }

    int getColumn() const {
        return column;
    }

    void setMat();
    void printMat() const;
};

Matrix::Matrix(int row, int column) : row(row), column(column) {
    M = new int* [row];
    for (int i = 0; i < row; i++) {
        M[i] = new int[column];
    }
    cout << "Constructor called";
}

Matrix::~Matrix() {
    for (int i = 0; i < row; i++) {
        delete[] M[i];
    }
    delete[] M;
    cout << "Destructor called";
}

void Matrix::setMat() {
    for (int i = 0; i < row; i++) {
        cout << "Enter the first row";
        for (int j = 0; j < column; j++) {
            M[i][j] = j;
        }
    }
}

void Matrix::printMat() const {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            cout << M[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
}

int main(){
    int numR, numC;
    cout << "Enter the number of rows: ";
    cin >> numR;
    cout << "Enter the number of column: ";
    cin >> numC;
    Matrix a(numR, numC);
    cout << a.getRow();
    a.setMat();
    a.printMat();
    return 0;
}

如果您真的想为 rowcolumn 成员使用单独的 setter,请尝试更像这样的方法:

#include <iostream>
#include <cmath>

using namespace std;

class Matrix
{
private:
    int row;
    int column;
    int** M;

    void checkMat();

public:
    Matrix();
    ~Matrix();

    void setRow(int row);
    void setColumn(int column);

    int getRow() const {
        return row;
    }

    int getColumn() const {
        return column;
    }

    void setMat();
    void printMat() const;
};

Matrix::Matrix() : row(0), column(0), M(NULL) {
    cout << "Constructor called";
}

Matrix::~Matrix() {
    for (int i = 0; i < row; i++) {
        delete[] M[i];
    }
    delete[] M;
    cout << "Destructor called";
}

void Matrix::setMat() {
    for (int i = 0; i < row; i++) {
        cout << "Enter the first row";
        for (int j = 0; j < column; j++) {
            M[i][j] = j;
        }
    }
}

void Matrix::setRow(int row) {
    this->row = row;
    checkMat();
}

void Matrix::setColumn(int column) {
    this->column = column;
    checkMat();
}

void Matrix::checkMat() {
    if ((!M) && (row > 0) && (column > 0)) {
        M = new int*[row];
        for (int i = 0; i < row; i++) {
            M[i] = new int[column];
        }
    }
}

void Matrix::printMat() const {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            cout << M[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
}

int main(){
    int numR, numC;
    Matrix a;
    cout << "Enter the number of rows: ";
    cin >> numR;
    a.setRow(numR);
    cout << "Enter the number of column: ";
    cin >> numC;
    a.setColumn(numC);
    cout << a.getRow();
    a.setMat();
    a.printMat();
    return 0;
}