• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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
0
hari4130
Asked:
hari4130
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
0
 
hari4130Author Commented:
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
0
 
hari4130Author Commented:
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
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
0
 
rcarlanCommented:
Hari,

The solution to
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21377420.html 
did not use void*. It was a type-safe implementation relying on templates. See the implementation of test at the end of the code below:


template <typename T>
class FuncCall
{
     public:
          typedef void (*FuncType)(T*, int);
          FuncCall(FuncType fn, T& a) : m_fn(fn), m_a(a) {}

          void operator() (int n)
          {
               m_fn(&m_a, n);
          }

     private:
          FuncType m_fn;
          T& m_a;
};


class A
{
     public:
          A() : n(0) {}
          int n;
};

class B
{
     public:
          B() : n(0) {}
          int n;
};

void FuncA(A* pa, int n)
{
     pa->n = n;
}

void FuncB(B* pb, int n)
{
     pb->n = n;
}


template <typename T>
void Execute(FuncCall<T>& fn)
{
     for (int i=0; i<10; ++i)
     {
          fn(i);
     }
}

void test()
{
     A a;
     B b;

     Execute(FuncCall<A>(FuncA, a));
     Execute(FuncCall<B>(FuncB, b));
}
 
Radu
0
 
hari4130Author Commented:
Thanks for the suggestions Radu.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now