C ++模板-多种类型 [英] C++ Template - Multiple types
本文介绍了C ++模板-多种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下类模板:
template <class T>
class MyClass
{
void MyFunc();
};
template <class T>
void MyClass<T>::MyFunc()
{
//...implementation goes here
}
我需要添加另一个函数 MyFunc2 ,该函数接受附加的模板参数T2 即
I need to add another function MyFunc2 which accepts an additional template argument T2 i.e.,
template <class T>
class MyClass
{
void MyFunc();
template <class T2>
static void MyFunc2(T2* data);
};
template <class T>
void MyClass<T>::MyFunc()
{
//...implementation goes here
}
template <class T, class T2>
void MyClass<T>::MyFunc2(T2* pData)
{
//...implementation goes here
}
我正在使用VS 2008编译器.我收到错误消息:
I am using VS 2008 compiler. I am getting the error:
error C2244: unable to match function definition to an existing declaration
在这种情况下,函数的定义和声明应该是什么样子.
How should the function's definition and declaration look like in this case.
推荐答案
template <class T>
template <class T2>
void MyClass<T>::MyFunc2(T2* pData)
{
//...implementation goes here
}
$ 14.5.2/1-模板可以是在一个或多个类中声明模板;这样的模板称为成员模板.会员模板可以在其类别之内或之外定义定义或类模板定义. a的成员模板在外部定义的类模板其类模板定义应用指定该类的模板参数模板,然后是成员的模板参数模板."
这篇关于C ++模板-多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文