2012-12-20 61 views
2

說我有:模板類專業化

template < typename T > 
class ClassA 
{ 
    void doTheStuff (T const * t); 
}; 

template < typename T > 
class ClassB 
{ 
    // Some stuff... 
}; 

我想專門的doTheStuff方法是這樣的ClassB的模板的所有實例:

template <typename T> 
void ClassA< ClassB<T> >::doTheStuff (ClassB<T> const * t) 
{ 
    // Stuff done in the same way for all ClassB<T> classes 
} 

當然,但,這是行不通的。恥辱是我不知道我該怎麼做。

與Visual Studio的編譯器,我得到:

error C2244: 'ClassA<T>::doTheStuff' : unable to match function definition to an existing declaration 
see declaration of 'ClassA<T>::doTheStuff' 
    definition 
    'void ClassA<ClassB<T>>::doTheStuff(const ClassB<T> *)' 
    existing declarations 
    'void ClassA<T>::doTheStuff(const T *)' 

我發現這個職位:Templated class specialization where template argument is a template

於是,我就滿級的專業化的建議,但它不工作之一:

template <> 
template < typename U > 
class ClassA< ClassB<U> > 
{ 
public: 
    void doTheStuff (ClassB<U> const * b) 
    { 
     // Stuff done in the same way for all ClassB<T> classes 
    } 
}; 

可視爲:

error C2910: 'ClassA<ClassB<U>>' : cannot be explicitly specialized 

歡迎任何幫助!

Floof。

回答

2

刪除額外template<>它會工作。

+0

謝謝!它確實有效,我不知道爲什麼他們在另一個線程上有這個額外的模板。 –