typedef float T;
typedef unsigned int uint;
namespace glgui
{
template <uint D>
struct _glgMatrix;
template <uint D>
struct _glgMatrixBase
{
protected:
T elem[D][D];
public:
_glgMatrixBase() {}
public:
inline T& operator() (uint r, uint c)
{ return elem[r][c]; }
inline const T& operator() (uint r, uint c) const
{ return elem[r][c]; }
inline _glgMatrix<D> operator* (T value) const;
};
template <uint D>
inline _glgMatrix<D> _glgMatrixBase<D>::operator* (T value) const
{
_glgMatrix<D> m;
for(int i = 0; i < D; ++i)
for(int j = 0; j < D; ++j)
m(i, j) = elem[i][j] * value;
return m;
}
// derived class used to specialize for certain sizes
template <uint D>
struct _glgMatrix: public _glgMatrixBase<D>
{
};
class A {};
// 3x3 specialization for manipulating 2D points
template <>
struct _glgMatrix<3U>: public _glgMatrixBase<3U>
{
inline _glgMatrix operator* (const A& a) const;
};
template <>
inline _glgMatrix<3U> _glgMatrix<3U>::operator* (const A& a) const
{
return _glgMatrix();
}
typedef _glgMatrix<3> glgMatrix33;
typedef _glgMatrix<4> glgMatrix44;
}
// then in a cpp file
void f()
{
glgMatrix44 m44;
m44 = m44 * 2.0f;
glgMatrix33 m33;
m33 = m33 * 2.0f;
}
Operator overloading/inheritance in class template
Maybe I'm missing something obvious here but I've checked this over and over and am sure it should work...
This is a cut down version of my matrix classes:
And the error is on the last line of the function f above:
error C2679: binary '*' : no operator defined which takes a right-hand operand of type 'const float' (or there is no acceptable conversion)
So _glgMatrix<4> inherits the operator fine, but the overloaded operator defined in the specialization for _glgMatrix<3> (with argument const A& a) seems to hide the initial operator defined in _glgMatrixBase.
Why? What's going on??
Edit: forgot to say I'm using VC++6.
Please help.
Cameron
[edited by - kamrann on July 29, 2002 5:38:15 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement