在矩阵类中的一个内存问题,谢谢大家帮忙了
有关类定义如下:
class Matrix
{
private:
int m_nRows; /* the rows of matrix */
int m_nCols; /* the columns of matrix */
double **m_pData; /* matrix memory handle */
public:
/* constructor */
Matrix(int nRows,int nCols);
/* copy constructor */
Matrix(const Matrix &rhs);
/* destructor */
virtual ~Matrix();
private:
/* allocate memory */
void _newMemory();
/* deallocate memory */
void _deleteMemory();
};
void Matrix::_newMemory()
{
m_pData=new double*[m_nRows];
for(int i=0;i<m_nCols;i++)
{
m_pData[i]=new double[m_nCols];
}
}
void Matrix::_deleteMemory()
{
if(m_pData!=NULL)
{
for(int i=0;i<m_nRows;i++)
{
if(NULL!=m_pData[i])
{
delete []m_pData[i];
m_pData[i]=NULL;
}
}
delete []m_pData;
m_pData=NULL;
}
}
Matrix::Matrix(int nRows,int nCols)
:m_nRows(nRows),m_nCols(nCols),m_pData(NULL)
{
_newMemory();
}
/* destructor */
Matrix::~Matrix()
{
_deleteMemory();
}
运行上面的代码,总是提示在删除内存的时候出现错误。。
大家看看,这个究竟是什么问题!!!