C++ 代码给出 EXC_BAD_ACCESS。

C++ code gives EXC_BAD_ACCESS.

如您所见,这是一个简单的 Matrix 模板。

当我尝试 运行 使用基本的主要功能时,我收到一条错误消息 "EXC_BAD_ACCESS"。我查看了错误并阅读了一些关于它的内容,但我在这里找不到我的问题。我是新手,这是我的任务。我错过了截止日期,但我想知道我错过了什么......

 template <class T>
    class Matrix
    {
    private:
        T** data; // matrix elements stored here
        int rows; // number of rows
        int cols; // number of columns
    public:
        Matrix(int numRows = 0, int numCols = 0); // makes storage allocation but leaves it uninitialized, for 0,0 dont allocate memory
        Matrix(T const* const* inputData, int numRows, int numCols);
        Matrix(const Matrix& rhs);
       ~Matrix();

    Matrix& operator=(const Matrix& rhs);


    Matrix operator+(const Matrix& rhs) const; // element-wise addition of two matrices
    Matrix operator-(const Matrix& rhs) const; // element-wise subtraction of two matrices
    Matrix operator*(const Matrix& rhs) const; // multiplication of two matrices, dot product


    T operator()(int r, int c) const; // returns the element value at row r and column c
    T& operator()(int r, int c); // returns reference of the element value at row r and column c

     int getRows() const; // returns the number of rows
     int getCols() const; // returns the number of columns

    void print() const; // prints the matrix with each column element separated by a tab and each row element in a new line print a newline after the last row
};





#include <iostream>


//parameter constructor
template<typename T>
Matrix<T>::Matrix(int _rows, int _cols) {


    rows = _rows;
    cols = _cols;
    T** data;
    data=new T *[_rows];
    for (int i=0; i< _rows; i++) {
        data[i]=new T [_cols];
    }

}



//copy constructor

template<typename T>
Matrix<T>::Matrix(const Matrix<T>& rhs) {

    data=new T*;
    rows = rhs.getRows();
    cols = rhs.getCols();

}

//destructor

template<typename T>
Matrix<T>::~Matrix() {}

//assignment operator

template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& rhs) {
    if (&rhs == this)
        return *this;

    int new_rows = rhs.getRows();
    int new_cols = rhs.getCols();

    data=new T *(new_rows);
    for (int i=0; i<rhs.size(); i++) {
        data[i].resize(new_cols);
    }

    for (int i=0; i<new_rows; i++) {
        for (int j=0; j<new_cols; j++) {
            data[i][j] = rhs(i, j);
        }
    }
    rows = new_rows;
    cols = new_cols;

    return *this;

}

//matematical operands
//1. addition

template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix& rhs) const {

    Matrix<T> result(*this);
    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            result(i,j) = this->data[i][j] + rhs(i,j);
        }
    }

    return result;
}

//2.subtraction

template<typename T>
Matrix<T> Matrix<T>::operator-(const Matrix& rhs) const {
    Matrix result(rows, cols);

    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            result(i,j) = this->data[i][j] - rhs(i,j);
        }
    }

    return result;
}

//3.multiplication

template<typename T>
Matrix<T> Matrix<T>::operator*(const Matrix& rhs) const {
    int rows = rhs.getRows();
    int cols = rhs.getCols();
    Matrix result(rows, cols);

    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            for (int k=0; k<rows; k++) {
                result(i,j) = this->data[i][k] * rhs(k,j);
            }
        }
    }

    return result;
}

//access the elements

template<typename T>
T Matrix<T>::operator()(int r, int c) const{
    return this->data[r][c];
}

//returns the reference value of element

template<typename T>
T& Matrix<T>::operator()(int r, int c){
    return this->data[r][c];
}

//number of rows
template<typename T>
int Matrix<T>::getRows() const {
    return this->rows;
}

//number of cols
template<typename T>
int Matrix<T>::getCols() const{
    return this->cols;
}

//printing
template <typename T>
void Matrix<T>::print() const{
    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            std::cout<<this->data[i][j]<<"/t";
            std::cout<<std::endl;
        }
    }
}

我发现您的代码中有太多与处理 data 的动态内存分配相关的错误。通过对私有数据使用 std::vector,可以 fixed/easily 补救其中的大部分问题。

而不是:

T** data; // matrix elements stored here
int rows; // number of rows
int cols; // number of columns

使用:

std::vector<std::vector<T>> data;