Link to home
Start Free TrialLog in
Avatar of hari4130
hari4130

asked on

template function pointer problem

I am unable to build my code on VC++ .NET, but it builds PERFECTLY fine on VC++ 6.0.

The statement where I am getting error is:

template<class T> typedef void (*FnPointer)(T a, bool val);
// Here I am declaring a function pointer with function taking 2 arguments. Since the 1st parameter can be of "any class type", I am using a function template as well.

error message:
error: a typedef template is illegal
error: cannot be a template definition

I do not want to use void * instead of template to solve the problem as it gives other errors (unresolved external symbol error etc....)

Since my entire implementation is really big, I am attaching a small snippet that I was trying to build in .NET . IT can provide a reasonable idea abt my problem.
//start of snippet----------------------------------------------------------------
class class1;                // I did not provide the entire implementation and other classes used
class class2;              

void Function(class1* c, int i)          // Function that i will pass thru pointer
{          
return;    
}

void Function2(class2* c, int i)        
{          
return;    
}

// Here I am declaring a function pointer with function taking 2 arguments. Since the 1st parameter can be of "any class type", I am using a function template as well.
template<class T2> typedef void (*FnPointer)(T2 c, int i);   // place causing error


int Callfunction(FnPointer pfn)  //function taking a Function pointer as parameter
{
     return 1;
}

void test()
{
    Callfunction((FnPointer) Function);  // passing the function
}
//end of snippet-----------------------------------------------------------

If you try to build this snippet in VS .NET, it will show the error mentioned above. Please provide me with a solution to this problem. any alternatives or fixes.
it would be great if  i am able to using function pointer and function template together.


Thanks
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Avatar of hari4130
hari4130

ASKER

The link is the same snippet which my friend posted earlier.

But the solution seems to be using void* as a solution. i would not want to use void * since it causes some other errors (unresolved external symbol error).

i would like to use the function template and fucntion pointers

if you build the snippet on .NET, you would get the error that i mentioned. Please attach a modified snippet that solves this problem without using void *

Thanks

hari
since i forgot to mention in my snippet,

the "class* c" is provided in the test implementation to the function so that the appropiate class pointer type is determined.

Avatar of Axter
You're making the code far more complicated then it has to be.

Just use a template on the function that takes the function pointer.
Example:

template<class T2>
int Callfunction(T2 pfn)  //function taking a Function pointer as parameter
{
      return 1;
}


Just keep it simple
ASKER CERTIFIED SOLUTION
Avatar of rcarlan
rcarlan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the suggestions Radu.