Sign in to follow this  
sobeit

operator overload problem

Recommended Posts

sobeit    463
template<typename T>
CMatrix<T> &CMatrix<T>::operator =( CMatrix<T> &AA)
{
	if( row != AA.getCMatrixRow () || column != AA.getCMatrixColumn ())
	{
		cout << "Matrices are not matching!";
		return *this;
	}

	else 
	{
		for( int i = 0; i < row; i++)
		{
			for( int j = 0; j < column; j++)
			{
				data[i][j] = AA.data[i][j];
			}
		}

		return *this;
	}
}

CMatrix<T> CMatrix<T>::operator +( CMatrix<T> &A)
{
	if( row != A.getCMatrixRow () || column != A.getCMatrixColumn ())
	{
		cout << "Matrices are not matching!";
		return *this;
	}

	else 
	{
		CMatrix<T> temp( row, column);

		for( int i = 0; i < row; i++)
		{
			for( int j = 0; j < column; j++)
			{
				temp.data[i][j] = data[i][j] + A.data[i][j];
			}
		}

		return temp;
	}
}

these are my "=","+" overload, is there something wrong? cause my program always breaks in "data[i][j] = AA.data[i][j];".

Share this post


Link to post
Share on other sites
Nanoha    2682
What exactly does "break" mean? does it not compile? does it give any error messages? What are the values of i and j when it breaks? what are the matrix sizes?

Also I'd suggest passing the other matrix in as a const (thats usually what a copy/assign operator does). Only thing I can think of is you have the row/column the wrong way round in the loop but as I can't see any other of your code I can't be sure.

Share this post


Link to post
Share on other sites
sobeit    463
you mind see my entire head and source code? when executing to this step "A=B+C", program has a interrupt in "CMatrix<T> &operator =( CMatrix<T> &AA);".( A,B,C are all CMatrix<T> type)
template <typename T>
class CMatrix
{
public:
CMatrix( int = 0, int = 0, float = 0.0);
~CMatrix();

int getCMatrixRow();
int getCMatrixColumn();
T **getCMatrixData();
int setCMatrixDiagonal(const T *);
int setCMatrixDiagonal(T);
int setCMatrixRow(int, const T *);
int setCMatrixColumn(int, const T *);
int setCMatrixElement( int, int, T);
int assignDataToOtherDP( T **);
int assignPartialMatrix( CMatrix<T> &, int);

CMatrix<T> &operator =( CMatrix<T> &AA);
CMatrix<T> &operator =( T **A);
CMatrix<T> &operator -();
CMatrix<T> operator +( CMatrix<T> &A);
CMatrix<T> operator -( CMatrix<T> &AA);
CMatrix<T> operator *( CMatrix<T> &A);

private:
int row, column;
T **data;

};
static void Release( float ** pp, int n)
{
for( int i = 0; i < n; i++)
{
delete []pp[i];
}

delete []pp;
}

static float ** matrixPlus( float **a, float **b, int n)
{
float **consequenceP;
consequenceP = new float *[n];
for( int i = 0; i < n; i++)
{
consequenceP[i] = new float[n];
}

for( int i = 0; i< n; i++)
{
for( int j = 0; j< n; j++)
{
consequenceP[i][j] = a[i][j] + b[i][j];
}
}

return consequenceP;
}

static float ** matrixMulti( float **a, float **b, int n)
{
float **consequenceM;
consequenceM = new float *[n];
for( int i = 0; i < n; i++)
{
consequenceM[i] = new float [n];
}

if( n > 2)
{
float **a_11, **a_12, **a_21, **a_22;
float **b_11, **b_12, **b_21, **b_22;
float **c_11, **c_12, **c_21, **c_22;

a_11 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
a_11[i] = new float[n/2];
}

a_12 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
a_12[i] = new float[n/2];
}

a_21 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
a_21[i] = new float[n/2];
}

a_22 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
a_22[i] = new float[n/2];
}

b_11 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
b_11[i] = new float[n/2];
}

b_12 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
b_12[i] = new float[n/2];
}

b_21 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
b_21[i] = new float[n/2];
}

b_22 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
b_22[i] = new float[n/2];
}

c_11 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
c_11[i] = new float[n/2];
}

c_12 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
c_12[i] = new float[n/2];
}

c_21 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
c_21[i] = new float[n/2];
}

c_22 = new float *[n/2];
for( int i = 0; i < n/2; i++)
{
c_22[i] = new float[n/2];
}

for( int i = 0; i < n/2; i++)
{
for( int j = 0; j < n/2; j++)
{
a_11[i][j] = a[i][j];
a_12[i][j] = a[i][n/2 + j];
a_21[i][j] = a[n/2 + i][j];
a_22[i][j] = a[n/2 + i][n/2 + j];
}
}

for( int i = 0; i < n/2; i++)
{
for( int j = 0; j < n/2; j++)
{
b_11[i][j] = b[i][j];
b_12[i][j] = b[i][n/2 + j];
b_21[i][j] = b[n/2 + i][j];
b_22[i][j] = b[n/2 + i][n/2 + j];
}
}

c_11 = matrixPlus( matrixMulti( a_11, b_11, n/2), matrixMulti( a_12, b_21, n/2), n/2);
c_12 = matrixPlus( matrixMulti( a_11, b_12, n/2), matrixMulti( a_12, b_22, n/2), n/2);
c_21 = matrixPlus( matrixMulti( a_21, b_11, n/2), matrixMulti( a_22, b_21, n/2), n/2);
c_22 = matrixPlus( matrixMulti( a_21, b_12, n/2), matrixMulti( a_22, b_22, n/2), n/2);

for( int i = 0; i < n/2; i++)
{
for( int j = 0; j < n/2; j++)
{
consequenceM[i][j] = c_11[i][j];
consequenceM[i][j + n/2] = c_12[i][j];
consequenceM[i + n/2][j] = c_21[i][j];
consequenceM[i + n/2][j + n/2] = c_22[i][j];
}
}

Release( a_11, n/2);
Release( a_12, n/2);
Release( a_21, n/2);
Release( a_22, n/2);
Release( b_11, n/2);
Release( b_12, n/2);
Release( b_21, n/2);
Release( b_22, n/2);
Release( c_11, n/2);
Release( c_12, n/2);
Release( c_21, n/2);
Release( c_22, n/2);

return consequenceM;
}

else
{
consequenceM[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0];
consequenceM[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1];
consequenceM[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0];
consequenceM[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1];

return consequenceM;
}
}



#include"CMatrix.h"

template <typename T>
CMatrix<T>::CMatrix( int r, int c, float initial)
{
row = r;
column = c;

data = new T*[row];
for( int i = 0; i < row; i++)
{
data[i] = new T[column];
}

for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
data[i][j] = initial;
}
}
}

template<typename T>
CMatrix<T>::~CMatrix()
{
for( int i = 0; i < 0; i++)
delete []data[i];
delete []data;
}

template<typename T>
int CMatrix<T>::getCMatrixRow()
{
return row;
}

template<typename T>
int CMatrix<T>::getCMatrixColumn()
{
return column;
}

template<typename T>
T **CMatrix<T>::getCMatrixData()
{
return data;
}

template<typename T>
int CMatrix<T>::setCMatrixDiagonal(const T *Array)
{
for( int i = 0, j = 0; i < row; i++, j++)
{
data[i][j] = Array[i];
}

return 0;
}

template<typename T>
int CMatrix<T>::setCMatrixDiagonal( T element)
{
for( int i = 0, j = 0; i < row; i++, j++)
{
data[i][j] = element;
}

return 0;
}

template<typename T>
int CMatrix<T>::setCMatrixRow( int r, const T *Array)
{
for( int j = 0; j < row; j++)
{
data[r][j] = Array[j];
}

return 0;
}

template<typename T>
int CMatrix<T>::setCMatrixColumn ( int c, const T *Array)
{
for( int i = 0; i < column; i++)
{
data[i][c] = Array[i];
}

return 0;
}

template<typename T>
int CMatrix<T>::setCMatrixElement ( int r, int c, T element)
{
data[r][c] = element;

return 0;
}

template<typename T>
int CMatrix<T>::assignDataToOtherDP ( T **P)
{
T **temp = new T *[row];
for( int i = 0; i < 0; i++)
temp[i] = new T [column];

for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
temp[i][j] = data[i][j];
}
}

P = temp;

return 0;
}

template<typename T>
int CMatrix<T>::assignPartialMatrix( CMatrix<T> &A, int flag)
{
switch(flag)
{
case 1:
for( int i = 0; i < row - 1; i++)
{
for( int j = i + 1; j < column; j++)
{
data[i][j] = A.data[i][j];
}
}
return 0;

case 2:
for( int i = 0, j = 0; i < row; i++, j++)
data[i][j] = A.data[i][j];
return 0;

case 3:
for( int i = 1; i < row; i++)
for( int j = 0; j < i; j++)
data[i][j] = A.data[i][j];
return 0;

default:
return 0;
}

}

template<typename T>
CMatrix<T> &CMatrix<T>::operator =( CMatrix<T> &AA)
{
if( row != AA.getCMatrixRow () || column != AA.getCMatrixColumn ())
{
cout << "Matrices are not matching!";
return *this;
}

else
{
for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
data[i][j] = AA.data[i][j];
}
}

return *this;
}
}

template<typename T>
CMatrix<T> &CMatrix<T>::operator =( T **A)
{
for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
data[i][j] = A[i][j];
}
}

return *this;
}

template<typename T>
CMatrix<T> &CMatrix<T>::operator -()
{
for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
data[i][j] = -data[i][j];
}
}

return *this;
}

template<typename T>
CMatrix<T> CMatrix<T>::operator +( CMatrix<T> &A)
{
if( row != A.getCMatrixRow () || column != A.getCMatrixColumn ())
{
cout << "Matrices are not matching!";
return *this;
}

else
{
CMatrix<T> temp( row, column);

for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
temp.data[i][j] = data[i][j] + A.data[i][j];
}
}

return temp;
}
}

template<typename T>
CMatrix<T> CMatrix<T>::operator -( CMatrix<T> &AA)
{
if( row != AA.getCMatrixRow () || column != AA.getCMatrixColumn ())
{
cout << "Matrixs are not matching!";
return *this;
}

else
{
CMatrix<T> temp( row, column);

for( int i = 0; i < row; i++)
{
for( int j = 0; j < column; j++)
{
temp.data[i][j] = data[i][j] + AA.data[i][j];
}
}

return temp;
}
}

template<typename T>
CMatrix<T> CMatrix<T>::operator *( CMatrix<T> &A)
{
T **consequence;
consequence = new T*[row];
for( int i = 0; i < row; i++)
consequence[i] = new T[row];

consequence = matrixMulti( data, A.data, row);

CMatrix<T> temp(row, column);
temp = consequence;

return temp;
}


Share this post


Link to post
Share on other sites
ppgamedev    311
in operator + code:
CMatrix<T> temp( row, column);
...
return temp;

shouldn't it be?:
CMatrix<T>* temp = new CMatrix<T>( row, column);
for( int i = 0; i < row; i++) {
for( int j = 0; j < column; j++) {
temp->data[i][j] = data[i][j] + A.data[i][j];
}
}
return *temp;

Share this post


Link to post
Share on other sites
jyk    2094
A few comments on your code:

1. I suggest dropping the 'C' prefix for your class names; IMO, it does not convey any useful information.

2. Presumably this is intended to be more of a 'general linear algebra' matrix class than a game/graphics oriented matrix class right? The reason I ask is that a game-oriented matrix class would not typically be implemented using dynamic allocations as shown here (mostly for reasons of efficiency).

3. For at least a few reasons (cache locality, less fragmentation of memory, etc.), matrix data of this sort is usually stored as a single 1-d array rather than as a 'jagged' 2-d array. Element access is then usually exposed via the () operator or via [][] notation (the latter is a little trickier to implement than the former - see boost::multi_array for an example of how it can be done).

4. Once you're sure you have a handle on the memory management side of things, I'd recommend swapping out the manual memory management for an RAII container such as std::vector.

5. It looks like you have an error in your destructor, here:
for( int i = 0; i < 0; i++)
6. Why do some of your functions have int return values, but only ever return 0?

7. Your setCMatrixDiagonal() function appears to assume that the matrix is square.

8. It doesn't look as if you've followed the Rule of Three, which is likely to cause problems.

There appears to be a lot of questionable memory management stuff going on here, and it looks like there are some potential ownership and general pointer-related issues as well. Based on a quick scan of your code, I'm fairly certain there are more problems here than those that I mentioned above.

Getting all of this stuff right 'manually' is a good exercise, but for production code, I think you'd want to take a different approach than what you've shown here. Switching to std::vector for your array data storage will probably solve about 90% of the problems (and potential problems) with your code, so that'd probably be a good place to start.

Share this post


Link to post
Share on other sites
sobeit    463
thanks a lot. but i'm still confused, as a tyro i think i should read more books and do more exercise. by the may, do you have a matrix class? i think it would be more explicit with a correct matrix class.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by sobeit
thanks a lot. but i'm still confused, as a tyro i think i should read more books and do more exercise. by the may, do you have a matrix class? i think it would be more explicit with a correct matrix class.
See my post in your other thread...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this