2011-09-16 127 views
6

我正在爲C++ name-demangling代碼編寫一些測試用例,並且在嘗試編譯時出現一個奇怪的錯誤:(以下是我在實踐中從不會使用的病態的錯誤C++代碼)。C++模板問題

template<class U, class V> 
class TStruct 
{ 
    U u; 
    V v; 
public: 
    void setU(const U& newu) {u = newu; } 
}; 

template<class T, class U> 
class Oog 
{ 
    T t; 
    U u; 

public: 
    Oog(const T& _t, const U& _u) : t(_t), u(_u) {} 
    void doit(TStruct<T,U> ts1, TStruct<U,T> ts2, U u1, T t1) {} 

    template<class F> 
    class Huh 
    { 
     F f; 
    public: 

     template<class V> 
     class Wham 
     { 
      V v; 
     public: 
      Wham(const V& _v) : v(_v) {} 
      void joy(TStruct<T,V> ts1, U u, F f) {} 
     }; 
    }; 
    int chakka(const Huh<T>::Wham<U>& wu, T t) {} // error here 
}; 

錯誤如下:

"typetest.cpp", line 165: error: nontype "Oog<T, U>::Huh<F>::Wham [with F=T]" 
    is not a template 

任何想法如何,我可以修復?

回答

7

正確的路線應儘可能,

int chakka(const typename Huh<T>::template Wham<U>& wu, T t) ... 
    it's a type ^^^^^^^^   ^^^^^^^^ indicate that 'Wham' is a template 

[注:g++ is quite helpful in this case :)]

+1

謝謝!我知道'typename',但不知道你必須以這種方式使用'template'關鍵字。 –

+0

+1:另一個很好的答案!這周至少有兩次! :D –

+0

@Tomalak,你給出了「很好的回答」評論,然後在有人反對之後收回你的投票/評論! – iammilind

2

你需要告訴它咦的威猛樂隊成員將是一個模板:

const Huh<T>::template Wham<U> & 
+1

你仍然缺少'typename'(至少[gcc抱怨](http://www.ideone.com/jjasO))。 – iammilind

+1

@iammilind是的,這並沒有發生在我身上。在我確定需要之前,必須盯着你的答案一會兒。 –

-1

這應該夠了(依賴類型導致麻煩)

int chakka(const typename Huh<T>::Wham<U>& wu, T t) {}

+0

不是。到達那裏,但! (你應該在發佈之前試試你的答案) –

+0

嗯..其實我試過了。我認爲只有VS 2010不需要'template'關鍵字。 – Werolik

+1

請在nextr downvoting之前在VS 2010中嘗試一下。 – Werolik