最近在写一些代码,用到的矩阵,但是现在也懒得费脑去使用库,就索性自己写了点。但是遇到了怎么都过不了的问题,不管是使用一维还是二维指针,析构函数都不能使用delete[]去释放内存,运营到这句程序就会停止,不管是限制这个指针是不是空的都没有用。很奇怪的是这个问题只会在矩阵乘法的时候遇到,希望大家给点意见啊。
下面不是我写的代码,是一个网友的,有矩阵的逆,后面会用到,暂时记下来
有人给我回复了 原来是忽略了深拷贝和浅拷贝啊
这个乘法是按值返回,需要拷贝构造函数,你自己不写的话编译器就会自动给你生成一个。问题是他那个是浅拷贝,对于这种new指针的肯定会出问题。
应该自己写拷贝构造函数和赋值运算符,做深拷贝。
#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <iostream>
#include <iomanip>
using namespace std;
class matrixobj
{
float matrix[3][3];
float cofactor(int i,int j);
public:
void displayMatrix();
void readMatrix();
friend matrixobj operator+(matrixobj &x, matrixobj &y);
friend matrixobj operator-(matrixobj &x, matrixobj &y);
friend matrixobj operator*(matrixobj &x, matrixobj &y);
friend matrixobj operator/(matrixobj &x, float d);
float matrixDeterminant();
matrixobj matrixInverse();
matrixobj matrixTranspose();
};
void matrixobj::displayMatrix()
{
int i,j;
for (i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
cout<<setw(4)<<matrix[i][j];
cout<<"\n"<<endl;
}
}
void matrixobj::readMatrix()
{
int i,j;
float x=0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
cin>>x;
matrix[i][j] = x;
}
}
float matrixobj::cofactor(int i,int j)
{
float cofactorValue;
int a1,b1,c1,d1,a2,b2,c2,d2;
a1 = (i + 1) % 3;
b1 = (j + 1) % 3;
c1 = (i + 2) % 3;
d1 = (j + 2) % 3;
a2 = (i + 2) % 3;
b2 = (j + 1) % 3;
c2 = (i + 1) % 3;
d2 = (j + 2) % 3;
cofactorValue = matrix[a1][b1] * matrix[c1][d1] - matrix[a2][b2] * matrix[c2][d2];
return cofactorValue;
}
float matrixobj::matrixDeterminant()
{
float det;
det = matrix[0][0] * cofactor(0,0);
det += matrix[0][1] * cofactor(0,1);
det += matrix[0][2] * cofactor(0,2);
return det;
}
matrixobj matrixobj::matrixInverse()
{
float det;
int i,j;
matrixobj z,y;
det = matrixDeterminant();
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = cofactor(i,j);
z = z.matrixTranspose();
if (det == (float) 0)
{
exit(0);
}
y = z / det;
return y;
}
matrixobj matrixobj::matrixTranspose()
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[j][i] = matrix[i][j];
return z;
}
matrixobj operator+(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j];
return z;
}
matrixobj operator-(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] - y.matrix[i][j];
return z;
}
matrixobj operator*(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j,k;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = 0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
for(k = 0; k < 3; k++)
z.matrix[i][j] += x.matrix[i][k] * y.matrix[k][j];
return z;
}
matrixobj operator/(matrixobj &x, float d)
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] / d;
cout<<"\n"<<endl;
return z;
}
#endif
//主程序
//两个矩阵的加,减,乘 及 矩阵转置
#include <stdio.h>
#include <conio.h>
#include "matrix.h"
#include <float.h>
#include <iostream>
using namespace std;
void main()
{
matrixobj a,b,c;
float x;
cout<<"Enter a 3 by 3 matrix (A): \n";
a.readMatrix();
cout<<"Enter a 3 by 3 matrix (B): \n";
b.readMatrix();
cout<<"Matrix (A) is : \n";
a.displayMatrix();
cout<<"Matrix (B) is : \n";
b.displayMatrix();
cout<<"Matrices (A) + (B) is : \n";
c = a + b;
c.displayMatrix();
getchar();
cout<<"Matrices (A) - (B) is : \n";
c = a - b;
c.displayMatrix();
getchar();
cout<<"Matrices (A) * (B) is : \n";
c = a * b;
c.displayMatrix();
getchar();
x = a.matrixDeterminant(); //矩阵行列式
getchar();
//c = a.matrixInverse();
c=a.matrixTranspose();
//cout<<"The Inversed Matrix of (A) is : \n";
cout<<"The Transposed Matrix of (A) is : \n";
c.displayMatrix();
getchar();
}