|
-
May 9th, 2005, 10:15 AM
#1
Thread Starter
Not NoteMe
Matrix Class, =, =*, * operators
I've got a matrix class, that manages an 1D float array (using indexing to make it act as a 2D array).
I'm having trouble implementing various operators. The problem i get is that it's trying to delete the array twice.
I've stepped through the code, and i still don't see why it's calling the deconstructor when it does.
Code:
int main(void)
{
CMatrix testMat(2,2,1.0f);
CMatrix testMat2(2,2,1.0f);
testMat = testMat * testMat2; //Destructor called after this line
testMat.Print();
return 0;
}
/////////////////////////
// Function prototypes in header file
/////////////////////////
CMatrix operator*(const float Value);
CMatrix operator*(const CMatrix &RHS); //Matrix Multiply
CMatrix &operator*=(const CMatrix &RHS); //Matrix Multiply
CMatrix &operator*=(const float Value);
CMatrix &operator=(const CMatrix &RHS); //To duplicate a matrix
/////////////////////////
// End Function prototypes
/////////////////////////
CMatrix::CMatrix()
{
m_pMatrix = NULL;
m_pInvMatrix = NULL;
m_pTranMatrix = NULL;
m_pAdjMatrix = NULL;
}
CMatrix::CMatrix(int Rows, int Columns)
{
m_pMatrix = new float[Rows*Columns];
m_pInvMatrix = new float[Rows*Columns];
m_pTranMatrix = new float[Rows*Columns];
m_pAdjMatrix = new float[Rows*Columns];
m_bInvIsDirty = m_bTranIsDirty = m_bDetIsDirty = m_bAdjIsDirty = true;
m_iColumns = Columns;
m_iRows = Rows;
}
CMatrix::CMatrix(int Rows, int Columns, float Value)
{
m_pMatrix = new float[Rows*Columns];
m_pInvMatrix = new float[Rows*Columns];
m_pTranMatrix = new float[Rows*Columns];
m_pAdjMatrix = new float[Rows*Columns];
for(int i=0;i<Rows*Columns;i++)
m_pMatrix[i] = Value;
m_bInvIsDirty = m_bTranIsDirty = m_bDetIsDirty = m_bAdjIsDirty = true;
m_iColumns = Columns;
m_iRows = Rows;
}
CMatrix::CMatrix(int Rows,int Columns,float *Matrix)
{
m_pMatrix = new float[Rows*Columns];
m_pInvMatrix = new float[Rows*Columns];
m_pTranMatrix = new float[Rows*Columns];
m_pAdjMatrix = new float[Rows*Columns];
for(int i=0;i<Rows*Columns;i++)
m_pMatrix[i] = Matrix[i];
m_bInvIsDirty = m_bTranIsDirty = m_bDetIsDirty = m_bAdjIsDirty = true;
m_iColumns = Columns;
m_iRows = Rows;
}
CMatrix::~CMatrix()
{
delete[] m_pMatrix;
delete[] m_pInvMatrix;
delete[] m_pTranMatrix;
delete[] m_pAdjMatrix;
}
CMatrix CMatrix::operator*(const float Value)
{
CMatrix Result(m_iColumns,m_iRows,m_pMatrix);
for(int i=0;i<m_iRows*m_iColumns;i++)
m_pMatrix[i] *= Value;
return Result;
}
CMatrix CMatrix::operator*(const CMatrix &RHS)
{
CMatrix Result(m_iRows,RHS.m_iColumns);
for(int i=0;i<m_iRows;i++)
for(int j=0;j<RHS.m_iColumns;j++)
for(int k=0;k<m_iColumns;k++)
Result.m_pMatrix[i+j*RHS.m_iColumns] += m_pMatrix[i+k*m_iColumns] * RHS.m_pMatrix[k+j*m_iColumns];
return Result;
}
CMatrix &CMatrix::operator*=(const CMatrix &RHS)
{
assert(RHS.m_iColumns == m_iColumns);
CMatrix tmp(m_iRows,RHS.m_iColumns);
tmp = *this * RHS;
memcpy(m_pMatrix,tmp.m_pMatrix,sizeof(float)*m_iColumns*m_iRows);
m_bInvIsDirty = m_bTranIsDirty = m_bDetIsDirty = m_bAdjIsDirty = true;
return *this;
}
CMatrix &CMatrix::operator*=(const float Value)
{
for(int i=0;i<m_iRows*m_iColumns;i++)
{
m_pMatrix[i] *= Value;
}
return *this;
}
CMatrix &CMatrix::operator=(const CMatrix &RHS)
{
//Copy the Columns and Rows of the matrix
m_iColumns = RHS.m_iColumns;
m_iRows = RHS.m_iRows;
delete[] m_pMatrix;
delete[] m_pInvMatrix;
delete[] m_pTranMatrix;
delete[] m_pAdjMatrix;
m_pMatrix = new float[m_iRows*m_iColumns];
m_pInvMatrix = new float[m_iRows*m_iColumns];
m_pTranMatrix = new float[m_iRows*m_iColumns];
m_pAdjMatrix = new float[m_iRows*m_iColumns];
//Copy the matrix array
memcpy(m_pMatrix,RHS.m_pMatrix,sizeof(float)*m_iColumns*m_iRows);
//Copy the cached matrices dirty flag values, and the cached matrices themselves if appropriate
m_bInvIsDirty = RHS.m_bInvIsDirty;
if(!m_bInvIsDirty)
memcpy(m_pInvMatrix,RHS.m_pInvMatrix,sizeof(float)*m_iColumns*m_iRows);
m_bTranIsDirty = RHS.m_bTranIsDirty;
if(!m_bTranIsDirty)
memcpy(m_pTranMatrix,RHS.m_pTranMatrix,sizeof(float)*m_iColumns*m_iRows);
m_bAdjIsDirty = RHS.m_bAdjIsDirty;
if(!m_bAdjIsDirty)
memcpy(m_pAdjMatrix,RHS.m_pAdjMatrix,sizeof(float)*m_iColumns*m_iRows);
//Copy the cached determinent flag and value if appropriate
m_bDetIsDirty = RHS.m_bDetIsDirty;
if(!m_bDetIsDirty)
m_fDet = RHS.m_fDet;
return *this;
}
Any help would be greatly appreciated.
Last edited by SLH; May 9th, 2005 at 10:36 AM.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|