J
jr.freester
I have created to classes Matrix and System. System is made up of
type matrix.
----------------------------------------------------------------------------------
class Matrix
{
private:
int row, col;
double *data
public:
Matrix(const int& M, const int& N): row(M), col(N)
{
data = new double[row*col];
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*row + n] = 0;
}
}
...... etc
};
----------------------------------------------------------------------------------
class System
{
private:
int num_eq; //number of differential equations in
system
Matrix *equations; //array of class Matrix
public:
/*Constructor*/
System(const unsigned int num):num_eq(num)
{
equations = new Matrix[num_eq];
}
..... etc
};
In both classes I have overloaded an operator to facilitate addition.
----------------------------------------------------------------------------------
/*Addition Operator Matrix plus Matrix*/
friend Matrix operator +(const Matrix& A, const Matrix& B)
{
Matrix C(A.row, A.col);
assert((A.row == B.row) && (A.col == B.col));
for(int m = 0; m<C.row; m++)
{
for(int n = 0; n<C.col; n++)
{
C.data[m*C.col + n] = A.data[m*A.col + n] +
B.data[m*B.col + n];
}
}
return C;
}
----------------------------------------------------------------------------------
/*Addition Operator System plus System*/
friend System operator +(const System& A, const System& B)
{
System C(A.num_eq);
assert(A.num_eq == B.num_eq);
for(int i = 0; i<C.num_eq; i++)
{
C.equations = A.equations + B.equations;
}
return C;
}
I have written a simple driver program to test the functionality of
these two classes. Given the following variables which have been
properly initialized
Matrix A, B, C, D;
System W, X, Y, Z;
D = A + B + C; //produces valid results
Z = W + X; //produces valid results
Z = W + X + Y; //produces a segmentation fault
I have tried enclosing the terms within parenthesis but that does not
do anything. The code for the operator+ is nearly identical for both
classes. What causes the segmentation fault and how can I resolve it?
Justin
PS How do I inset code and preserve formatting?
type matrix.
----------------------------------------------------------------------------------
class Matrix
{
private:
int row, col;
double *data
public:
Matrix(const int& M, const int& N): row(M), col(N)
{
data = new double[row*col];
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*row + n] = 0;
}
}
...... etc
};
----------------------------------------------------------------------------------
class System
{
private:
int num_eq; //number of differential equations in
system
Matrix *equations; //array of class Matrix
public:
/*Constructor*/
System(const unsigned int num):num_eq(num)
{
equations = new Matrix[num_eq];
}
..... etc
};
In both classes I have overloaded an operator to facilitate addition.
----------------------------------------------------------------------------------
/*Addition Operator Matrix plus Matrix*/
friend Matrix operator +(const Matrix& A, const Matrix& B)
{
Matrix C(A.row, A.col);
assert((A.row == B.row) && (A.col == B.col));
for(int m = 0; m<C.row; m++)
{
for(int n = 0; n<C.col; n++)
{
C.data[m*C.col + n] = A.data[m*A.col + n] +
B.data[m*B.col + n];
}
}
return C;
}
----------------------------------------------------------------------------------
/*Addition Operator System plus System*/
friend System operator +(const System& A, const System& B)
{
System C(A.num_eq);
assert(A.num_eq == B.num_eq);
for(int i = 0; i<C.num_eq; i++)
{
C.equations = A.equations + B.equations;
}
return C;
}
I have written a simple driver program to test the functionality of
these two classes. Given the following variables which have been
properly initialized
Matrix A, B, C, D;
System W, X, Y, Z;
D = A + B + C; //produces valid results
Z = W + X; //produces valid results
Z = W + X + Y; //produces a segmentation fault
I have tried enclosing the terms within parenthesis but that does not
do anything. The code for the operator+ is nearly identical for both
classes. What causes the segmentation fault and how can I resolve it?
Justin
PS How do I inset code and preserve formatting?