1
template <class T>
class Test
{
public:
 template<class T>
 void f();  //If i define function here itself, error is not reported.
};

template <class T>
void Test<T>::f() 
{
}              //Error here.

int main()
{
 Test<float> ob;
 ob.f<int>();
}

It produces below error.

error C2244: 'Test<T>::f' : unable to match function definition to an
existing declaration

definition 'void Test::f(void)'
existing declarations 'void Test::f(void)'

Error says declaration and definitions have the same prototype but not matching. Why is this an error ? and how to solve it ?

If i define function within the class, it doesn't report error. But i want to define outside the class.

2 Answers 2

2

Change as

template <class T> 
class Test 
{ 
public: 
 template<class U> 
 void f(); 
}; 

template <class T> 
template<class U>
void Test<T>::f()  
{ 
} 
0
1
....
public:
  template<class T> // this shadows the previous declaration of typename T
   void f();  
};

Change the parameter name. Here is the working code.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.