2013-03-04 122 views
-1

在此设置:如何从模板派生类中的模板化基类调用成员?

template<int N> 
struct Base { 
    void foo(); 
}; 

class Derived : Base<1> { 
    static void bar(Derived *d) { 
     //No syntax errors here 
     d->Base<1>::foo(); 
    } 
}; 

,一切工作正常。然而,这个例子:

template<class E> 
struct Base { 
    void foo(); 
}; 

template<class E> 
class Derived : Base<E> { 
    static void bar(Derived<E> *d) { 
     //syntax errors here 
     d->Base<E>::foo(); 
    } 
}; 

我得到:

error: expected primary-expression before '>' token 
error: '::foo' has not been declared 

有什么区别?为什么第二个会导致语法错误?

+0

您使用的编译器是什么? – 2013-03-04 10:41:48

+0

织补,错字。这个问题是垃圾。 – Eric 2013-03-04 10:47:32

回答

1

随着你的代码编译上锵3.2(见here)和GCC 4.7.2精的前提下(见here),我没有看到一个理由使用Base<E>:::只需使用d->foo()

template<class E> 
struct Base { 
    void foo() { } 
}; 

template<class E> 
struct Derived : Base<E> { 
    static void bar(Derived<E> *d) { 
     //syntax errors here 
     d->foo(); 
    } 
}; 

int main() 
{ 
    Derived<int> d; 
    Derived<int>::bar(&d); 
} 

或者,您可以尝试使用template消歧器:

d->template Base<E>::foo(); 
相关问题