2011-11-28 144 views
3

我想顯式實例化類型U的模板化函數,類型爲T的模板類中。我的代碼如下生成一個警告和鏈接器沒有找到顯式實例ReinterpretAs()。任何人都可以發現錯誤或建議如何做到這一點?我使用上述VC++ 2010C++不同類型的模板類的顯式模板化函數實例化

template<typename T> 
class Matrix 
{ 
    public: 
    template<typename U> Matrix<U> ReinterpretAs() const; 
}; 

template<typename T> 
template<typename U> 
Matrix<U> Matrix<T>::ReinterpretAs() const 
{ 
    Matrix<U> m; 
    // ... 
    return m; 
} 


// Explicit instantiation. 
template class Matrix<int>; 
template class Matrix<float>; 

template Matrix<float> Matrix<int>::ReinterpretAs<float>(); 
template Matrix<int> Matrix<float>::ReinterpretAs<int>(); 

的最後兩行編譯器警告:

warning #536: no instance of function template "Matrix<T>::ReinterpretAs 
[with T=float]" matches the specified type 

謝謝你在前進,馬克

回答

8

你錯過了const

template class Matrix<int>; 
template class Matrix<float>; 

template Matrix<float> Matrix<int>::ReinterpretAs<float>() const; 
template Matrix<int> Matrix<float>::ReinterpretAs<int>() const; 
+0

謝謝!盯着這個方式太久了......呃! – Mark