c++ - 涉及指针和手动实现的矩阵类的问题

标签 c++ class pointers matrix dynamic-memory-allocation

我目前正在开发一个更大的项目,其中涉及实现线性代数计算器。我决定不使用任何其他现有的库,这些库可能会帮助我实现它,因为我认为这太容易了。

我首先开始编写 Matrix 类的代码,现在看起来像这样:

class Matrix{
private:
    int rows; // no. rows
    int columns; // no. columns
    double** matVal; //values of the matrix
    char name; //only used when printing it out or by outside programs.
public:
    //constructors and destructor
    Matrix();
    Matrix(int r,int c,char _name);
    ~Matrix();

    //basic get functions for private access
    int getNrRows();
    int getNrCols();
    char getName();
    double getVal(int row,int col);

    //basic set functions for private variables
    void setVal(int row,int col,double value);
    void setName(char _name);

    //basic matrix operations
    Matrix operator=(Matrix M);
    Matrix operator+(Matrix M);
    Matrix operator-(Matrix M);
    Matrix operator*(Matrix M);

    //Printing out the matrix
    void Print();
};

一开始很顺利,但后来我遇到了一个致命的错误,这让我无法进一步前进。有关更多信息,这里是我的函数(+一些代码来尝试找出问题所在)以及我在 main() 中执行的内容:

#define cout std::cout

Matrix::Matrix(){
    rows = 0;
    columns = 0;
    matVal = nullptr;
}

Matrix::Matrix(int r,int c,char _name){
    rows = r;
    columns = c;
    name = _name;
    matVal = new double*[r];
    for(int i = 0; i < r; i++){
        matVal[i] = new double[c];
    }
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            matVal[i][j] = 0;
        }
    }
}

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

int Matrix::getNrRows(){
    return rows;
}

int Matrix::getNrCols(){
    return columns;
}

char Matrix::getName(){
    return name;
}

double Matrix::getVal(int row, int col){
    return matVal[row-1][col-1];
}

void Matrix::setVal(int row,int col,double value){
    matVal[row-1][col-1] = value;
}

void Matrix::setName(char _name){
    name = _name;
}

Matrix Matrix::operator=(Matrix M){
    for (int i = 0; i < rows; i++)
        delete[] matVal[i];
    delete[] matVal;

    rows = M.rows;
    columns = M.columns;

    matVal = new double*[rows];

    for(int i = 0; i < rows; i++){
        matVal[i] = new double[M.columns];
    }

    for(int i = 0; i < M.rows; i++){
        for(int j = 0; j < M.columns; j++){
            matVal[i][j] = M.matVal[i][j];
            cout<<matVal[i][j]<<' ';
        }
        cout<<'\n';
    }
    cout<<this<<std::endl;
    return *this;
}

Matrix Matrix::operator+(Matrix M){
    Matrix Rez;
    Rez.rows = rows;
    Rez.columns = columns;
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            Rez.matVal[i][j] = matVal[i][j] + M.matVal[i][j];
        }
    }
    return Rez;
}

void Matrix::Print(){
    cout<<'\n';
    cout<<name<<": "<<"\n";
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            cout<<matVal[i][j]<<' ';
        }
        cout<<'\n';
    }
    cout<<'\n';
    return;
}

主要:

Matrix M(4,3,'A');
M.setVal(1,1,2);
M.setVal(1,3,-1.1);
M.Print();
Matrix A(4,3,'B');
A.setVal(3,2,5);
A.Print();
Matrix C(4,3,'C');
C = A;
cout<<C.getVal(3,2)<<'\n';
cout<<C.getNrCols()<<" "<<C.getNrRows()<<endl;
C.Print();
cout<<"S"<<endl;

打印前 2 个矩阵工作正常,当我在为它分配正确的值后在操作符 = 函数中打印 C 的每个元素时,它工作正常,但是当我在 C 上使用 Print() 函数时,它崩溃了。以下是上述代码的控制台输出:

A:
2 0 -1.1
0 0 0
0 0 0
0 0 0


B:
0 0 0
0 0 0
0 5 0
0 0 0

0 0 0
0 0 0
0 5 0
0 0 0
0x69fed0
5
3 4

C:

一开始我完全不知道为什么要这样做,但后来我打印了指向每个变量的指针(这次它打印了所有内容并返回 0):

A:
0x850e38 0x850e40 0x850e48
0x851318 0x851320 0x851328
0x851338 0x851340 0x851348
0x851358 0x851360 0x851368


B:
0x851390 0x851398 0x8513a0
0x8513b0 0x8513b8 0x8513c0
0x8513d0 0x8513d8 0x8513e0
0x855b08 0x855b10 0x855b18

0x855b40 0x855b48 0x855b50
0x855b60 0x855b68 0x855b70
0x855b80 0x855b88 0x855b90
0x855ba0 0x855ba8 0x855bb0
0x69fed0
5
3 4

C:
0 0x8 0x10
0 0x8 0x10
0 0x8 0x10
0 0x8 0x10

S

现在我认为 Print 函数有问题(否则为什么我能够在 main 中打印出 5?)。我仍然不知道到底发生了什么,所以我请求你的帮助。如果这是一个菜鸟错误,我很抱歉,我还很缺乏经验。

我还忘记补充一点,类和类函数位于单独的文件(头文件和 cpp)中,尽管我不知道这会如何影响事情。

最佳答案

在另一个答案中提出的签名Matrix operator=(Matrix&);完全错误。正确的签名应该是

Matrix& operator=(const Matrix&);

void operator=(const Matrix&);

如果您不需要链接分配 (a = b = c)。

您必须实现的最低限度:复制构造函数、复制赋值和析构函数。对于矩阵类来说,实现移动操作也是合理的。这称为 the rule of zero/three/five (在我们的例子中是五个):

If a class requires no user-defined constructors, no user-defined assignment operators and no user-defined destructor, don't define them; if a class requires a user-defined destructor, a user-defined copy (and move) constructor, or a user-defined copy (and move) assignment operator, it almost certainly requires all three (five).

假设矩阵内部表示为一维数组。这种方法避免了矩阵元素访问的不必要的间接并简化了代码。

class Matrix {
public:
    Matrix(const Matrix&);
    Matrix(Matrix&&);

    Matrix& operator=(const Matrix&);
    Matrix& operator=(Matrix&&);

    ~Matrix();

private:
    double* data_        = nullptr;
    std::ptrdiff_t rows_ = 0;
    std::ptrdiff_t cols_ = 0;
};

复制操作应该,即它们应该复制数据,而不仅仅是底层指针。让我们从复制构造函数开始。它应该分配存储空间,然后将数据从 other 复制到此存储空间:

Matrix(const Matrix& other) 
: rows_(other.rows_), cols_(other.cols_) {
    const auto n = other.rows_ * other.cols_;
    data_ = new double[n];
    std::copy(other.data_, other.data_ + n, data_);
}

现在让我们实现交换:

void swap(Matrix& other) {
    std::swap(rows_, other.rows_);
    std::swap(cols_, other.cols_);
    std::swap(data_, other.data_);
}

这个函数非常有用,因为它允许我们几乎不需要代码就可以实现移动构造函数、复制赋值和移动赋值:

Matrix(Matrix&& other) {
    swap(other);
}

Matrix& operator=(const Matrix& other) {
    Matrix(other).swap(*this);
    return *this;
}

Matrix& operator=(Matrix&& other) {
    Matrix(std::move(other)).swap(*this);
    return *this;
}

有了这样的规范实现,一旦您正确实现了复制构造函数和交换,您就可以确保这些函数能够正确实现(包括自赋值处理)。欣赏优雅copy-and-swap idiom .

现在我们来谈谈operator+。看看这个实现:

Matrix operator+(Matrix other) const {
    assert(rows_ == other.rows_);
    assert(cols_ == other.cols_);

    const auto n = rows_ * cols_;
    for (std::ptrdiff_t i = 0; i < n; ++i)
        other.data_[i] += data_[i];
    return other;
}

在这里,我们按值获取参数并获取它的(深层)拷贝。然后我们将 this->data_ 添加到拷贝中并返回该拷贝。无需引入另一个本地Matrix变量。

Complete demo

关于c++ - 涉及指针和手动实现的矩阵类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62369451/

相关文章:

c++ - 使用枚举值初始化静态全局常量。这样安全吗?陷阱?

c++ - 如何在 C++ 中获取指向 main() 方法的函数指针?

pointers - 将具有多个变量的结构写入单个字节数组

c++ - 编码正确逻辑时输出错误的原因?

c++ - 从元组中提取 vector

C++ - 以下代码会导致未定义的行为吗?

c++ - unordered_map 的问题

c# - DAL 类(class)应该公开吗?

java - 实现子类的好方法

c++ - 为什么要有指针参数?