473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
2 2356
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
3
by: Keith P. Boruff | last post by:
Hello all, Currently, in my work, I'm dealing with some old and sloppy C++ code (written before '93). I'm trying to put test harnesses around the stuff I'm working on (using Boost testsuite...
8
by: Gonçalo Rodrigues | last post by:
Hi all, I have a template class (call it Object) whose instances have a variable size part - an array of of T objects. But this variable size part is fixed at creation time so instead of...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
8
by: amirr | last post by:
Guys, As you know static member functions do not have a this pointer. so instead, what shall i do? for example ... consider I want to call Code: ( text ) 1.
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
2
by: webinfinite | last post by:
This is my first post here, please tell me if I did anything wrong. in the following code snippet: 1. template <class In, class Pred> 2. In find_if(In begin, In end, Pred f) { 3. while (begin...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
2
by: Bruce !C!+ | last post by:
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) =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.