指针矩阵执行错误
pointer matrix error on execution
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];
相关文章:
- 为什么g++在未执行的代码处标记强制转换错误
- 无法在我的堆排序代码中找到错误.无法正确执行.C++
- GCC:随机构建导致执行期间分段错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 在 c++ 中执行 BFS 时出现分段错误
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 甚至在开始执行之前出现分段错误
- 执行错误访问 遍历向量
- 从 C 可执行文件加载动态库时收到错误C++"undefined symbol"
- 解决方案在第 25 行执行错误'out of bounds'
- 表单显示对话框函数错误,并且不执行下面的语句
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- 这个编译器错误究竟希望我执行什么?
- 执行具有转换错误的过程
- -bash:/a.out:无法执行二进制文件:Exec格式错误
- curl_easy_在Linux上执行分段错误
- 是否可以使函数模板对不适当的类型而不是错误执行默认操作
- 分段错误执行方法
- 未处理的异常错误执行数学操作