基本上,我有一個矩陣類像這樣(有很多運算符重載等功能去掉):模板化矩陣類不產生正常運行
template
<
uint32 TRows,
uint32 TCols
>
struct Matrix
{
float values[TRows][TCols];
inline explicit Matrix()
{
}
inline Matrix<TRows - 1, TCols - 1> minor(const uint32 col, const uint32 row)
{
Matrix<TRows - 1, TCols - 1> matrix;
for(int i = 0; i < TRows; ++i)
for(int j = 0; j < TCols; ++j)
{
if(i == col || j == row) continue;
matrix.values[i - (i > col)][j - (j > row)] = this->values[i][j];
}
return matrix;
}
inline float determinant()
{
if(TRows != TCols) throw DimensionError("Matrix is not square");
float det = 0;
if(TRows <= 0)
det = 0;
else if(TRows == 1)
det = this->values[0][0];
else if(TRows == 2)
det = this->values[0][0] * this->values[1][1] - this->values[1][0] * this->values[0][1];
else
for(int j = 0; j < TCols; ++j)
det += (j % 2 ? -1 : 1) * this->values[0][j] * this->minor(0, j).determinant();
return det;
}
}
我不明白爲什麼,對於線det += (j % 2 ? -1 : 1) * this->values[0][j] * this->minor(0, j).determinant();
,GCC嘗試生成功能的巨大數額:
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -49u, unsigned int TCols = -49u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -48u, unsigned int TCols = -48u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -47u, unsigned int TCols = -47u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -46u, unsigned int TCols = -46u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -45u, unsigned int TCols = -45u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -44u, unsigned int TCols = -44u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -43u, unsigned int TCols = -43u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -42u, unsigned int TCols = -42u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -41u, unsigned int TCols = -41u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -40u, unsigned int TCols = -40u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -39u, unsigned int TCols = -39u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -38u, unsigned int TCols = -38u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -37u, unsigned int TCols = -37u]'
matrix.cpp:95: instantiated from `float Matrix<TRows, TCols>::determinant() [with unsigned int TRows = -36u, unsigned int TCols = -36u]'
這可能是我的代碼一定的誤差,但我不能爲我的生命看到我要去哪裏錯了。幫助將非常感謝!
謝謝!有用!只是在你的代碼中的一個小錯字 - 'int float'應該簡單地'float':) – rfw 2010-08-19 09:32:21
@rfw:這是一個動態時間分支,你需要一個靜態時間分支。爲0大小的矩陣添加專門化。 – GManNickG 2010-08-19 09:37:15