2010-10-31 115 views
3

我想創建一個處理整數,雙精度和字符串的泛型類。然而,試圖實例化模板類時,我得到了以下錯誤消息:雙模板類型?

error: 'double' is not a valid type for a template constant parameter 

實例化運作爲int類型完全沒問題,一樣的內部代碼,雖然我還沒有去到字符串類型尚未。看起來好像這應該沒問題,因爲你可以實例化向量等。有什麼我在這裏失蹤?

// file forest.h 

template<typename NODETYPE> class Forest 
{ 
    template<NODETYPE>            // Line 15 
    friend Forest<NODETYPE>& operator+(Forest<NODETYPE>& f1, 
             Forest<NODETYPE>& f2); 

    template<NODETYPE>            // Line 17 
    friend ostream& operator<<(ostream& output, 
           const Forest<NODETYPE>& f1); 

    template<NODETYPE>            // Line 19 
    friend void outputHelper(ostream& output, 
          const ForestNode<NODETYPE>& currentNode, 
          int depth); 
    /* ... */ 
}; 

錯誤發生如下:

\project 4\forest.h|341|instantiated from here| 
\project 4\forest.h|15|error: 'double' is not a valid type for a template constant parameter| 
\project 4\forest.h|17|error: 'double' is not a valid type for a template constant parameter| 
\project 4\forest.h|19|error: 'double' is not a valid type for a template constant parameter| 
+7

@joedillian:「有什麼我在這裏失蹤?」 - 是的。你在你的問題中缺少一些代碼。如果您可以向我們展示一個能夠再現問題的最小代碼段,這將非常有幫助。 – 2010-10-31 21:51:17

+1

你想要有模板類型參數,有模板常量參數還是做模板特化?他們是不同的東西。也許你打算做他們其中的一個,並做了另一個意外?請顯示一些代碼,我們會幫助你弄清楚你正在做什麼,以及你應該做什麼。 – 2010-10-31 21:53:08

+0

說實話,我真的不知道如何製造這個錯誤的縮影。上次我發佈時,我被告知我沒有收到任何幫助,因爲我的代碼示例太大。現在我被告知我會被忽略,因爲我無法提供代碼。 : - /我會暫時發佈我的全部代碼。 – joedillian 2010-10-31 21:59:24

回答

4
template<NODETYPE> friend Forest<NODETYPE>& operator+(Forest<NODETYPE>& f1, Forest<NODETYPE>& f2); 

    template<NODETYPE> friend ostream& operator<<(ostream& output, const Forest<NODETYPE>& f1); 

    template<NODETYPE> friend void outputHelper(ostream& output, const ForestNode<NODETYPE>& currentNode, int depth); 

這些朋友聲明無效。如果你有一個模板類,當你在它自己的範圍內引用它時,你不需要重複它的模板參數。即使你打算允許Forest的任何其他實例化,那麼你將不得不使用typename或class並且調用NODETYPE別的東西。

+0

感謝您的幫助,我理解我的錯誤是什麼,基本上我使用NODETYPE來指代森林的類型以及朋友函數中輸入的類型,對吧? – joedillian 2010-10-31 22:58:10

+0

@ joedillian:是的。 – Puppy 2010-11-01 10:53:45

3

可以使用double(或floatlong double)與任何編譯器,甚至有點接近於符合模板參數。你不能做的是使用浮點值作爲非型模板參數。

最接近你的是通常將浮點值傳遞給ctor,並將它存儲在你的對象中。

1

浮點值(如double)的模板常量參數被禁止。

template <double x> struct A {}; 

但是你可以實例化double類型的模板(你想要做的,如果我得到你的問題是什麼)。

template <typename T> struct A {}; 
... 
A<double> a; 

如果你想專注您對特定類型的模板雙,然後做

template <typename T> struct A {}; 
template <> struct A<double> {...}; 
+0

我的課程聲明爲模板,朋友功能函數聲明爲模板。然而,當我嘗試實例化類 * foo =新類()它表示double不是可接受的參數。 – joedillian 2010-10-31 22:23:36

+1

好的,我明白了。錯誤信息可能會引起誤解,但你不能做'template 朋友Forest &operator +(Forest &f1,Forest & f2);''就像'template fun(T t);'一個正確的原型看起來像'模板 fun(T t);' – log0 2010-10-31 22:38:14

2

你最有可能試圖做這樣的事情:

template_type<3.1415926d> blarg; 

的地方,不知何故。
這是不允許的。雙打(浮動,長雙打)不允許作爲模板常量參數。 現在的東西,你可能會遇到過是這樣的:

template_type<"life, the universe and everything"> blarg; 

這也是(由於某種原因)未admissable,由於指針類型應該有外部鏈接等等:

char* my_str="life, the universe and everything"; 
template_type<my_str> blarg; 

shoule只是細

現在作爲一個側面說明:一些編譯器做還是沒讓浮點常量(IIRC的gcc 3,或許還有其它)