Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Template - Multiple types

Tags:

c++

templates

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?

like image 793
SysAdmin Avatar asked Oct 21 '10 11:10

SysAdmin


People also ask

Which template can have multiple parameters?

C++ Class Template with multiple parameters You can also use multiple parameters in your class template.

How many types of templates are there in C?

Correct Option: C There are two types of templates. They are function template and class template.

How many template parameters are allowed in a template classes?

Explanation: Just like normal parameters we can pass more than one or more template parameters to a template class.

What is class template with multiple parameters explain with suitable example?

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.


1 Answers

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."

like image 158
Chubsdad Avatar answered Oct 05 '22 13:10

Chubsdad