C ++代码提供了EXC_BAD_ACCESS。

时间:2015-03-25 18:16:06

标签: c++ templates matrix exc-bad-access

正如您所看到的,它是一个简单的矩阵模板。

当我尝试使用基本主要功能运行时,我收到一个错误,说" 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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我发现代码中有太多与处理data动态内存分配相关的错误。通过将std::vector用于私人数据,可以修复/轻松修复其中的大部分内容。

而不是:

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

使用:

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