2012-05-30 40 views
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; 
} 

據我所知,這兩個實現沒有太多技術上的不同。主要的區別是,rotation使用一個額外的模板參數相比translation

然而,G ++不接受rotation我現在有它的方式:

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>' 

這是怎麼回事?

+0

尤其適用於模板發佈完成錯誤。 – tuxuday

回答

3

你的類只有一個模板參數,而第二個是指一個模板類的模板功能,所以你需要

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

這適用的功能是否是靜態或沒有。

+0

謝謝,這似乎是伎倆。奇怪,因爲我從舊的代碼庫複製了這些代碼,只做了一些重構。我不記得這在當時是一個問題...(將在3分鐘內接受,需要等待超時) – robrene

+0

@robrene也許你有一個不符合規範的編譯器w.r.t.這個問題在當天回來。 – juanchopanza

相關問題