Template question

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!
 
V

Victor Bazarov

Chris said:
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?


You need to specialise your Matrix<1> and that should be basically like
this:

template<> class Matrix<1> {
float value; // not an array
public:
...
float getDeterminant() const { return value; }
...
};

After which the compiler should never need to instantiate Matrix<0>.

Victor
 
C

Chris Simmons

Thanks a lot, that worked out well. I had thought about doing something
similar, but my idea was to only specialize the getDeterminant member
function, as in:
template<>
Matrix<2>::getDeterminant()

but I didn't think of specializing the class. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top