2013-08-22 81 views
3

我想定义一个模板类的特化,其中包含一个非类型模板参数成员函数。我得到以下错误:模板类与非类型模板参数成员函数

error: too few template-parameter-lists 

Here's a sample class that describes the problem in brief, 
// file.h 
template <typename T> 
class ClassA { 
    T Setup(); 
    template <int K> static void Execute(); 
}; 

//file.cc 
void ClassA<int>::Execute<2>() { //Do stuff } 

我相信这是比设计问题更多的语法问题,任何线索?由于

回答

5

即使你完全专注模板,你仍然需要template<>

template<> template<> void ClassA<int>::Execute<2>() { //Do stuff } 
+0

那么,抛出错误 错误:template-id'Execute <2>'for'void ClassA :: Execute()'与任何模板声明不匹配 – blueskin

+0

这是因为它需要说'template <>'两次(onc e为班级,一次为功能),如我的答案中所述。 –

+0

哎呀,错过了函数模板。谢谢你,@ MarkB –

4

你忘了告诉你的专业模板类的模板方法编译:

template <> 
template <> 
void ClassA<int>::Execute<2>() 
{ 
    //Do stuff 
}