2015-06-28 154 views
-2

我的代碼;錯誤:太多模板參數列表

template<typename T, int N> 
class ngon{ 
    point<T> vertices[N]; 
    ... 
    template<typename O> ngon<T,N>& operator=(const ngon<O,N> otyp); 
    // O stands for other, as in other type 
    ... 
}; 

... 
template<typename T, int N> typename<typename O> 
ngon<T,N>& operator=(const ngon<O,N> otyp){ 
    for (int i = 0; i < N; i++) 
    vertices[i] = point<T>(otyp.vertices[i]); 
    return this; 
} 

給出錯誤;

.\Libraries/.\Geometry\Polygon_D2.hpp:103:11: error: too many template-parameter-lists 
ngon<T,N>& operator=(const ngon<O,N> otyp){ 

我做錯了什麼?模板是完全正確的。

+0

這是什麼附加'typename '在'template typename '...? – vsoftco

+0

它自動化類型轉換 – user4578093

+0

我的意思是語法。它看起來像一個錯字,你在模板decl之外有一個'typename ',並且沒有依賴類型,所以不需要'typename'。 – vsoftco

回答

1

使用

ngon<T,N> ngon<T,N>::operator=(const ngon<O,N> otyp){ 

,而不是

ngon<T,N> operator=(const ngon<O,N> otyp){ 

編譯器首先把事實的運營商是在公共領域的說明,並有兩個模板列表而不是一個,而不是提的是,操作員無效。然後輸出錯誤的模糊錯誤,而不是檢測到函數沒有被列爲成員函數。

+0

我希望這可以幫助一些人。我總是複製我的類定義,然後在做大量樣板代碼時替換相關的字段,所以這種事情會隨着我的意願而下滑。 – user4578093