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;
}
}
当我编译它似乎没问题,但当我运行它不起作用,我不明白为什么...它说“程序停止工作”并返回错误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 Multiply(Matrix&amp;)。
答案 0 :(得分:2)
默认构造函数中存在错误:
val = new double*[1];
val[1] = new double[1];
1不是val的有效索引,应该是:
val[0] = new double[1];