tags:

views:

30

answers:

2

Hi, I have below code.

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  A: 

Change as

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

template <class T> 
template<class U>
void Test<T>::f()  
{ 
} 
Chubsdad
+1: working, Thank you.
bjskishore123
+1  A: 
....
public:
  template<class T> // this shadows the previous declaration of typename T
   void f();  
};

Change the parameter name. Here is the working code.

Prasoon Saurav
+1: for clarifying on shadowing concept.
bjskishore123