我正在为OpenGLES项目编写Math模块。我写了一个类来管理通用大小的浮点矩阵
template <unsigned int N>
class MatrixN {
public:
float m[N*N];
MatrixN(){}
virtual ~MatrixN(){}
void identify();
MatrixN& operator=(const MatrixN& b);
};
template <unsigned int N>
MatrixN<N> operator*(const MatrixN<N>& a, const MatrixN<N>& b);
//CPP file implementation
template<unsigned int N>
MatrixN<N> operator*(const MatrixN<N>&a, const MatrixN<N> &b) {
MatrixN<N> matrix;
for(unsigned int i = 0; i < N; i++){
for(unsigned int j = 0; j < N; j++){
matrix.m[i * N + j] = 0;
for(unsigned int z = 0; z < N; z++){
matrix.m[i * N + j] += a.m[i * N + z] * b.m[z * N + j];
}
}
}
return matrix;
}
我创建了一个子类来管理3x3矩阵
class Matrix3 : public MatrixN<3> {
public:
void rotateX(const float radians);
void rotateY(const float radians);
void rotateZ(const float radians);
};
为什么当我执行此操作时
//Rotations instances of Matrix3
Matrix3 rotation = this->rotation * input.rotation;
我在编译时收到此错误?
no viable conversion from 'MatrixN<3U>' to 'const Matrix3'
这是因为乘法运算返回MatrixN<3>
,而不是Matrix3
在这种情况下,您可以在Matrix3
该接受条件中创建一个构造函数MatrixN<3>
代码(未经测试):
class Matrix3 : public MatrixN<3> {
public:
Matrix3 (const MatrixN<3>& mat){/*set internal values*/}
void rotateX(const float radians);
void rotateY(const float radians);
void rotateZ(const float radians);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句