467,895 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

question about function pointer and template

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!
Oct 19 '08 #1
  • viewed: 2133
Share:
2 Replies
On 2008-10-19 04:10:08 -0400, "Bruce !C!+" <aa*******@163.comsaid:
>
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()’" ???
Because getOp takes no arguments, so a plain call like that one doesn't
provide any way to figure out what type T should be. Use getOp<int>().

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 19 '08 #2
Hi, peter. I have another question:
Can someone explain the code snippet below:

float (*getOp())(float, float)
{
return &Minus;
}

It seems that we define a function named " getOp ", and it's return type
is a function pointer "float (*opFun)(float, float)". But I wonder
whether the parameters will passed to "Minus ".

Thanks!

Oct 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Suzanne Vogel | last post: by
3 posts views Thread by Keith P. Boruff | last post: by
8 posts views Thread by Gonçalo Rodrigues | last post: by
7 posts views Thread by Markus Svilans | last post: by
8 posts views Thread by amirr | last post: by
12 posts views Thread by WaterWalk | last post: by
2 posts views Thread by webinfinite | last post: by
5 posts views Thread by Fei Liu | last post: by
2 posts views Thread by Bruce !C!+ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.