question about function pointer and template

B

Bruce !C!+

as we known , we can use function pointer as:
float Minus (float a, float b) { return a-b; }
float (*getOp())(float, float)
{
return &Minus;
}
int main()
{
float (*opFun)(float, float) = NULL;
opFun= getOp();
cout<< (*opFun)(3,4)<<endl;
}


but, if Minus() is a template such as

template <typename T>
T Minus (T a, T b) { return a-b; }

i had to write getOp() such as:

template <typename T>
T (*getOp())(T, T)
{
return &Minus;
}
int main()
{
cout<< (*getOp())(3,4)<<endl; //compile error: no matching function
for call to ‘getOp()’
}

why "no matching function for call to ‘getOp()’" ???
an if i write main() such as:

template <typename T> //compile error: expected primary-expression
before ‘template’; expected `;' before ‘template’
T (*opFun)(T, T) = NULL;
opFun=getOp(); //compile error:‘opFun’ was not declared in this
scope; no matching function for call to ‘getOp()’
cout<< (*opFun)(3,4)<<endl;

what shall i do? or how to edit my source code to fix this?
thanks all of you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top