Когда я запускаю этот код, компьютер показывает ошибку памяти
инструкция в x эталонной памяти в y. Память не может быть прочитана
#include<cstdlib>
using namespace std;
class Matrix{
private:
int row,col,**ptr;
public:
Matrix();
void create(int,int);
void show();
Matrix multiply(Matrix);
};
Matrix::Matrix()
{
row=0;
col=0;
ptr=NULL;
}
void Matrix::create(int r,int c)
{
row=r;
col=c;
ptr=new int* [row];
static srand(time(0));
for(int i=0;i<row;++i)
{
ptr[i]=new int[i+1];
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
ptr[i][j]=rand()%6+1;
}
}
}
Matrix Matrix::multiply(Matrix obj2)
{
if(col==obj2.row)
{
Matrix temp;
for(int i=0; i<row; ++i)
{
for(int j=0; j<obj2.col; ++j)
{
temp.ptr[i][j]=0;
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<obj2.col;j++)
{
for(int k=0;k<col;k++)
{
temp.ptr[i][j]+=ptr[i][k]*obj2.ptr[k][j];
}
}
}
return temp;
}
else
{
cout<<"Conditions are NOT fulfill for Multiplication"<<endl;
}
}
void Matrix::show()
{
cout<<"Matrix: "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<ptr[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
Matrix obj1,obj2,obj3;
obj1.create(2,2);
obj1.show();
obj2.create(2,2);
obj2.show();
obj3=obj1.multiply(obj2);
obj3.show();
}
Программа должна умножить матрицу и сохранить в 3-м объекте, но возникает ошибка. Я не знаю, где проблема. Вот почему я отправил весь код.