Consider the following class template:
template <class T> class MyClass { void MyFunc(); }; template <class T> void MyClass<T>::MyFunc() { //...implementation goes here }
I need to add another function template MyFunc2
which accepts a 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) // error here { //...implementation goes here }
I am using VS 2008 compiler. I am getting the error:
error C2244: unable to match function definition to an existing declaration
What should the function's definition and declaration look like in this case?
C++ Class Template with multiple parameters You can also use multiple parameters in your class template.
Correct Option: C There are two types of templates. They are function template and class template.
Explanation: Just like normal parameters we can pass more than one or more template parameters to a template class.
Explanation of the code: In the above program, the Test constructor has two arguments of generic type. The type of arguments is mentioned inside angle brackets < > while creating objects.
template <class T> template <class T2> void MyClass<T>::MyFunc2(T2* pData) { //...implementation goes here }
$14.5.2/1 - "A template can be declared within a class or class template; such a template is called a member template. A member template can be defined within or outside its class definition or class template definition. A member template of a class template that is defined outside of its class template definition shall be specified with the template-parameters of the class template followed by the template-parameters of the member template."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With