Trouble with template classes
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 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:
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.
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:
You want your template class declaration to be:
Code:
template <class T>
class Matrix4x4
{
T m[4];
// etc...
};
template <class T>
inline T& Matrix4x4<T>::operator[] (const unsigned j)
{
// etc...
}
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
to
Everything worked okay.
Thanks for the reply though.
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
Code:
inline Matrix4x4<real>::MatrixRow Matrix4x4<real>::operator[] (int i) {to
Code:
inline [b]typename[/b] Matrix4x4<real>::MatrixRow Matrix4x4<real>::operator[] (int i) {Everything worked okay.
Thanks for the reply though.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| C++ Interlocking Classes | merrill541 | 1 | 2,199 |
Jan 25, 2009 08:43 PM Last Post: akb825 |
|
| Noob: Accessing Structures from Cocoa Classes | MikeC | 15 | 6,574 |
Oct 19, 2007 02:42 PM Last Post: MikeC |
|
| Trouble With Template Classes in C++ | Nick | 4 | 2,841 |
Nov 21, 2006 10:25 AM Last Post: DoG |
|
| Template specialization of a method | Fenris | 4 | 2,764 |
Jul 11, 2005 02:45 PM Last Post: OneSadCookie |
|
| Noob: Copying Classes | hangt5 | 3 | 3,157 |
Mar 9, 2005 12:52 PM Last Post: codemattic |
|

