0

まず第一に、私のコードのスニペットはmatrix.hpp

template <class T>
class matrix
{
  public:
    matrix();

    T& operator() (size_t i, size_t j) {return elem[i+j*4];}
    const T& operator() (size_t i, size_t j) const {return elem[i+j*4];}

    // ...

    static matrix<T> identity() {return matrix<T>();}
    static matrix<T> translation(const vector<T>&);
    template <class RT> static matrix<T> rotation_x(RT);

    // ...

};

// ...

template <class T>
inline
matrix<T>
matrix<T>::translation(const vector<T>& v)
{
    matrix<T> result;
    result(0, 3) = v.x;
    result(1, 3) = v.y;
    result(2, 3) = v.z;
    return result;
}

template <class T, class RT>
inline
matrix<T>
matrix<T>::rotation_x(RT angle)
{
    RT ca = std::cos(angle);
    RT sa = std::sin(angle);
    matrix<T> result;
    result(1, 1) = +ca;
    result(1, 2) = -sa;
    result(2, 1) = +sa;
    result(2, 2) = +ca;
    return result;
}

私の知る限り、これら2つの実装は技術的にはそれほど違いはありません。主な違いはrotationtranslation

rotationそれでも、g++は私が現在持っている方法を受け入れません。

la/matrix.hpp:272:31: error: invalid use of incomplete type 'class la::matrix<T>'
la/matrix.hpp:16:7: error: declaration of 'class la::matrix<T>'

何が起きてる?

4

1 に答える 1

3

クラスにはテンプレート パラメータが 1 つしかなく、2 番目のパラメータはテンプレート クラスのテンプレート関数を参照するため、

template <class T>
template <class RT>
inline
matrix<T> matrix<T>::rotation_x(RT angle) { .... }

これは、関数が静的であるかどうかに関係なく適用されます。

于 2012-05-30T09:03:44.143 に答える