2011-09-16 131 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