我想创建一个泛型类,其中包含一个显示一条消息的方法,如果类的类型是int而另一个是double的。这里是我的代码:C++中成员函数模板的专门化
template<class T>
class A {
public:
template <T> void B();
};
template<class T>
void A<int>::B{
//some code here
}
template<class T>
void A<double>::B{
//some code here
}
我得到了以下错误:
'double': illegal type for non-type template parameter '__formal'
'A<int>::B': unable to match function definition to an existing declaration
先感谢您的任何解决方案。
您的代码不匹配你的错误。请提供[mcve] – NathanOliver
另外,您不应将类似模板参数名称用于功能模板作为类模板。 – NathanOliver
你确定要'B'也是一个模板吗?从你的描述看来,你似乎希望'B'是类模板'A'的普通非模板成员函数。 – Angew