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.
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.