C
Chris Simmons
Hi guys.
I'm trying to program a generic square matrix class using templates. The
general idea is that if I want a 4x4 matrix, I'll declare it as:
Matrix<4> matrix;
I've got everything working except calculating determinants. I'm trying
to use a recursive formula for the determinants. Here's my code:
/// Code to calculate the determinant of a matrix
/// variables: float values[order][order];
template <int order>
float Matrix<order>::getDeterminant() const {
float det = 0;
if (order == 2)
det = values[0][0] * values[1][1] - values[1][0] * values[0][1];
else
{
for ( int i = 0; i < order; ++i)
{
int sign = ( i % 2) ? -1 : 1;
Matrix<order - 1> cf = Matrix<order -1 >( *this, i, 0);
det += sign * values[0] * cf.getDeterminant();
}
}
return det;
}
/// End code
When I compile this under Visual C++ .Net 2003, it gives me the
following error:
"errorC2087: 'values': missing subscript"
I've tracked this down to the fact that for some reason the compiler is
writing the code for a Matrix<0> class, even though I'm never using one
(it shouldn't even make a Matix(1) class in the above example). Is there
any way to stop the compiler from generating all Matrix classes, like by
providing a "base case" type of thing?
Thanks in advance!
I'm trying to program a generic square matrix class using templates. The
general idea is that if I want a 4x4 matrix, I'll declare it as:
Matrix<4> matrix;
I've got everything working except calculating determinants. I'm trying
to use a recursive formula for the determinants. Here's my code:
/// Code to calculate the determinant of a matrix
/// variables: float values[order][order];
template <int order>
float Matrix<order>::getDeterminant() const {
float det = 0;
if (order == 2)
det = values[0][0] * values[1][1] - values[1][0] * values[0][1];
else
{
for ( int i = 0; i < order; ++i)
{
int sign = ( i % 2) ? -1 : 1;
Matrix<order - 1> cf = Matrix<order -1 >( *this, i, 0);
det += sign * values[0] * cf.getDeterminant();
}
}
return det;
}
/// End code
When I compile this under Visual C++ .Net 2003, it gives me the
following error:
"errorC2087: 'values': missing subscript"
I've tracked this down to the fact that for some reason the compiler is
writing the code for a Matrix<0> class, even though I'm never using one
(it shouldn't even make a Matix(1) class in the above example). Is there
any way to stop the compiler from generating all Matrix classes, like by
providing a "base case" type of thing?
Thanks in advance!