PDA

View Full Version : Trouble with template classes


ermitgilsukaru
2006.08.07, 07:31 PM
Hi. I'm trying to convert my math-classes into templates and am having some troubles.

I have a Matrix4x4 class with column-major data (because OpenGL wants it like that). But I prefer to view the matrix as row-major. Therefore I made an inner class, MatrixRow, to enable the C-style M[row][column] indexing.

This all works fine when the code isn't templated, but I can't get it to compile when I convert it to a template.

On to the code:


/// The homogenous coordinate transformation matrix. The matrix elements can be
/// accessed in C style M[row][column].
template <typename real>
class Matrix4x4 {
public:
// --------------- Data ------------------- //
real m[16];
// ---------------------------------------- //

/// Helper-class for Matrix4x4 that makes the Matrix4x4 look like
/// a row major array from the outside, instead of the actual column
/// major layout that OpenGL needs.
class MatrixRow {
friend class Matrix4x4<real>;
public:
real& operator[] (int j);

private:
real* rowBase;
MatrixRow() {} // Should not be created by anything besides the
// Matrix4x4 class.
};


MatrixRow operator[] (int i);
};


template <typename real>
inline real& Matrix4x4<real>::MatrixRow::operator[] (int j) {
assert(j < 4);

return *(rowBase + (j * 4));
}

template <typename real>
inline Matrix4x4<real>::MatrixRow Matrix4x4<real>::operator[] (int i) {
assert(i < 4);

MatrixRow row;
row.rowBase = m + i;

return row;
}


The class is heavily edited. I'm only showing the relevant code.

I'd really appreciate if somebody could tell me what's wrong with this code. Also, feel free to rip on my design decisions. I'm new at this and, truth be told, I suck.

jfaller
2006.08.11, 01:50 PM
It's been a while since I read my C++ standard, but the typename qualifier is for something different. (It's really for specifing confusing statements to the compiler when the compiler cannot tell if you are talking about a function, variable, or typname.) I suspect that's your problem.

You want your template class declaration to be:


template <class T>
class Matrix4x4
{
T m[4];
// etc...
};

template <class T>
inline T& Matrix4x4<T>::operator[] (const unsigned j)
{
// etc...
}

ermitgilsukaru
2006.08.11, 06:00 PM
The keywords typename and class are actually interchangeable in template declararations, at least according to everything I've read about templates. All my other math classes compiled with no problem with the typename keyword.

I usually use class in templates, but I thought typename showed my intentions more clearly in this instance, because this template really is only supposed to be used with the native types.

The problem however was solved by different usage of the typename keyword, though the problem wasn't superflous typenames, but rather not enough of them.

When I changed

inline Matrix4x4<real>::MatrixRow Matrix4x4<real>::operator[] (int i) {

to

inline typename Matrix4x4<real>::MatrixRow Matrix4x4<real>::operator[] (int i) {

Everything worked okay.

Thanks for the reply though.