指针矩阵执行错误

pointer matrix error on execution

本文关键字:错误 执行 指针      更新时间:2023-10-16
        class Matrix{
            double **val;
            int rows,cols,errorCode;
        public:
            Matrix();
            Matrix(int);
            Matrix(int, int);
            ~Matrix();
            void Print();
            void Read();
            double Get(int, int);
            void Assign(int,int,double);
            void Addition(Matrix&);
            void Subtraction(Matrix&);
            Matrix Multiply(Matrix&);
            void Multiply(double);
        };
    Matrix::Matrix(){
        //First array go with rows.
        val = new double*[1];
        val[1] = new double[1];
        rows = 1;
        cols = 1;
        errorCode = 0;
    }
    Matrix::Matrix(int n){
        rows = n;
        cols = n;
        val = new double*[rows];
        for(int i = 0;i<rows;i++){
            val[i] = new double[cols];
        }
    }
    Matrix::Matrix(int n,int m){
        rows = n;
        cols = m;
        val = new double*[rows];
        for(int i = 0;i<rows;i++){
            val[i] = new double[cols];
        }
    }
    Matrix::~Matrix(){
        for(int i=0;i<rows;i++){
            delete[] val[i];
        }
        delete[] val;
    }
Matrix Matrix::Multiply(Matrix &a){
            if(cols != a.rows){
                Matrix b;
                b.errorCode=111; //That means dimensions are not valid;
                return b;
            }
            else{
                Matrix b(rows,a.cols);
                double  p;
                for(int i=0;i<rows;i++){
                    for(int j=0;j<a.cols;j++){
                        p=0;
                        for(int k=0;k<cols;k++){p += val[i][k]*a.val[k][j];}
                        b.Assign(i,j,p);
                    }
                }
                return b;
            }
        }

当我编译它似乎是好的,但当我运行它不工作,我不明白为什么…它显示"program stopped worked"并返回错误255

如果需要,我可以把我写的所有代码…

我添加了构造函数…

不幸的是,我不能发布图像…它说进程返回255,这是一个来自Windows的警告,程序不能正常工作,它停止工作…

这里是所有的代码,如果有人有时间,如果它有助于你更好地理解。
#include <iostream>
using namespace std;
class Matrix{
    double **val;
    int rows,cols,errorCode;
public:
    Matrix();
    Matrix(int);
    Matrix(int, int);
    ~Matrix();
    void Print();
    void Read();
    double Get(int, int);
    void Assign(int,int,double);
    void Addition(Matrix&);
    void Subtraction(Matrix&);
    Matrix Multiply(Matrix&);
    void Multiply(double);
};
Matrix::Matrix(){
    //First array go with rows.
    val = new double*[1];
    val[0] = new double[1];
    rows = 1;
    cols = 1;
    errorCode = 0;
}
Matrix::Matrix(int n){
    rows = n;
    cols = n;
    val = new double*[rows];
    for(int i = 0;i<rows;i++){
        val[i] = new double[cols];
    }
}
Matrix::Matrix(int n,int m){
    rows = n;
    cols = m;
    val = new double*[rows];
    for(int i = 0;i<rows;i++){
        val[i] = new double[cols];
    }
}
Matrix::~Matrix(){
    for(int i=0;i<rows;i++){
        delete[] val[i];
    }
    delete[] val;
}
void Matrix::Read(){
    for(int i=0;i<rows;i++){
        delete[] val[i];
    }
    delete[] val;
    cout<<"Give the matrix dimensions (rows and cols) : ";
    cin>>rows>>cols;
    val = new double*[rows];
    for(int i = 0;i<rows;i++){
        val[i] = new double[cols];
    }
    cout<<"Give the matrix values: "<<endl;
    for(int i = 0;i<rows;i++){
        for(int j = 0;j<cols;j++){ cin>>val[i][j];}
    }
}
void Matrix::Print(){
    cout<<"The matrix with "<<rows<<" rows and "<<cols<<" cols, have next values: "<<endl;
    for(int i = 0;i<rows;i++){
        for(int j = 0;j<cols;j++){ cout<<val[i][j]<<" ";}
        cout<<endl;
    }
}
double Matrix::Get(int n, int m){
    return val[n-1][m-1];
}
void Matrix::Assign(int n,int m,double v){
    val[n-1][m-1] = v;
}
void Matrix::Addition(Matrix &a){
    if(rows != a.rows || cols !=a.cols){
        errorCode=111; //That means dimensions are not equal;
    }
    else{
        for(int i = 0;i<rows;i++){
            for(int j = 0;j<cols;j++){ val[i][j] += a.val[i][j];}
        }
    }
}

void Matrix::Subtraction(Matrix &a){
    if(rows != a.rows || cols !=a.cols){
        errorCode=111; //That means dimensions are not equal;
    }
    else{
        for(int i = 0;i<rows;i++){
            for(int j = 0;j<cols;j++){ val[i][j] -=a.val[i][j];}
        }
    }
}
void Matrix::Multiply(double x){
     for(int i = 0;i<rows;i++){
            for(int j = 0;j<cols;j++){ val[i][j] *= x;}
        }
}
Matrix Matrix::Multiply(Matrix &a){
    if(cols != a.rows){
        errorCode=111; //That means dimensions are not equal;
        Matrix b;
        return b;
    }
    else{
        Matrix b(rows,a.cols);
        double  p;
        for(int i=0;i<rows;i++){
            for(int j=0;j<a.cols;j++){
                p=0;
                for(int k=0;k<cols;k++){p += val[i][k]*a.val[k][j];}
                b.Assign(i,j,p);
            }
        }
        return b;
    }
}
int main(){
Matrix a,b(2,3);
b.Assign(1,1,0);
b.Assign(1,2,3);
b.Assign(1,3,5);
b.Assign(2,1,5);
b.Assign(2,2,5);
b.Assign(2,3,2);
b.Print();
a.Read();
a.Print();
cout<<endl;
//when i read a i put 3x2 matrix, so the multiplying can be done.
Matrix c=a.Multiply(b);
c.Print();
return 0;
}

问题出现时,我试图使用函数:矩阵乘法(Matrix&)。

默认构造函数出错:

    val = new double*[1];
    val[1] = new double[1];

1不是val的有效索引,它应该是:

    val[0] = new double[1];