我试图通过为matrice操作创建一个类来理解C ++类。我发现了转储错误!我收到了编译器的以下文献:
terminate called after throwing an instance of 'std:bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
这是我的编程:
#include <stdio.h>
#include <cmath>
#include <iostream>
using namespace std;
void swap(double &a, double &b)
{
double temp = a;
a = b;
b = temp;
}
class matric
{
public:
int row;
double *pData = new double[row *row];
void input();
void output();
int InverseMatrix(matric m1);
int det(matric m1);
void multiply(matric m1, matric m2);
};
void matric::input()
{
int i, j;
cout << "enter the size of matrix:";
cin >> row;
cout << "enter the matrix:" << endl;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
cin >> pData[i * row + j];
}
}
}
void matric::output()
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
cout << pData[i * row + j] << " ";
}
cout << endl;
}
}
void matric::multiply(matric m1, matric m2)
{
int j1 = 0, k1 = 0, j2 = 0, k2 = 0;
for (j1 = 0; j1 < row; j1++)
{
for (k2 = 0; k2 < row; k2++)
{
*(pData + j1 * row + k2) = 0;
for (k1 = 0; k1 < row; k1++)
{
*(pData + j1 * row + k2) += *(m1.pData + j1 * row + k1) * (*(m2.pData + j2 * row + k2));
j2++;
}
j2 = 0;
}
}
}
int matric::InverseMatrix(matric m1)
{
double *m = new double[row * row];
double *ptemp, *pt = m;
int i, j;
ptemp = pData;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
*pt = *ptemp;
ptemp++;
pt++;
}
}
int k;
int *is = new int[row], *js = new int[row];
for (k = 0; k < row; k++)
{
double max = 0;
for (i = k; i < row; i++)
{
for (j = k; j < row; j++)
{
if (fabs(*(m + i * row + j)) > max)
{
max = *(m + i * row + j);
is[k] = i;
js[k] = j;
}
}
}
if (0 == max)
{
return 1;
}
if (is[k] != k)
{
for (i = 0; i < row; i++)
{
swap(*(m + k * row + i), *(m + is[k]*row + i));
}
}
if (js[k] != k)
{
for (i = 0; i < row; i++)
{
swap(*(m + i * row + k), *(m + i * row + js[k]));
}
}
*(m + k * row + k) = 1 / (*(m + k * row + k));
for (j = 0; j < row; j++)
{
if (j != k)
{
*(m + k * row + j) *= *((m + k * row + k));
}
}
for (i = 0; i < row; i++)
{
if (i != k)
{
for (j = 0; j < row; j++)
{
if (j != k)
{
*(m + i * row + j) -= *(m + i * row + k) **(m + k * row + j);
}
}
}
}
for (i = 0; i < row; i++)
{
if (i != k)
{
*(m + i * row + k) *= -(*(m + k * row + k));
}
}
}
int r;
for (r = row - 1; r >= 0; r--)
{
if (js[r] != r)
{
for (j = 0; j < row; j++)
{
swap(*(m + r * row + j), *(m + js[r]*row + j));
}
}
if (is[r] != r)
{
for (i = 0; i < row; i++)
{
swap(*(m + i * row + r), *(m + i * row + is[r]));
}
}
}
ptemp = pData;
pt = m;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
*ptemp = *pt;
ptemp++;
pt++;
}
}
delete []is;
delete []js;
delete []m;
return 0;
}
int main()
{
matric m1;
m1.input();
m1.output();
matric m2;
m2.input();
m2.output();
cout << "creating m3" << endl;
matric m3;
m3.input();
m3.multiply(m1, m2);
m3.output();
matric m4;
m4.InverseMatrix(m1);
m4.output();
return 0;
}
答案 0 :(得分:1)
首先删除你班级的初始化。
double *pData = new double[row *row];
只是这个
double *pData;
然后为您的类创建一个合适的构造函数,它将使用row
初始化变量0
,并使用pData
初始化NULL
。如果您需要它,请创建一个构造函数,以便像这样
matric(int newSize) {
// allocate memoty here
}