473,396 Members | 2,016 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,396 software developers and data experts.

function pointers as arguments

CS
Hi,

How do you pass a function pointer as an argument?

I have the following:

class FReport
{
...
};

// function object used to retrieve reports
//------------------------------------------------------------
class FindReportByID : public std::unary_function<FReport*, bool>
{
public:
FindReportByID(STD_int id): m_ID(id) {};
inline bool operator () (const FReport* pReport) const;
private:
const STD_int m_ID;
};

and the function to find a report using a function object:

FReport* findReport(std::unary_function<FReport*,bool>&
rFunctionObject) const
{
// ReportIt is a typedef list iterator
const ReportIt pos = std::find_if(m_reports.begin(),
m_reports.end(), rFunctionObject);
return *pos;
}

(The idea is to have a function that finds something using a given
function object)

and what I try to do:

int id = 1; // a report id
FindReportByID findByID(id); // my function object
FReport* pReport = findReport(findByName); // ???!!!! error here C2064:
term does not evaluate to a function
What am I doing wrong?

Thanks.

Mar 24 '06 #1
6 2685
CS wrote:
Hi,

How do you pass a function pointer as an argument?


http://www.google.com/search?q=funct...s+as+arguments

http://www.newty.de/fpt/fpt.html#passPtr

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 24 '06 #2
* CS:
Hi,

How do you pass a function pointer as an argument?

I have the following:

class FReport
{
...
};

// function object used to retrieve reports
//------------------------------------------------------------
class FindReportByID : public std::unary_function<FReport*, bool>
{
public:
FindReportByID(STD_int id): m_ID(id) {};
inline bool operator () (const FReport* pReport) const;
private:
const STD_int m_ID;
};

and the function to find a report using a function object:

FReport* findReport(std::unary_function<FReport*,bool>&
rFunctionObject) const
{
// ReportIt is a typedef list iterator
const ReportIt pos = std::find_if(m_reports.begin(),
m_reports.end(), rFunctionObject);
return *pos;
}

(The idea is to have a function that finds something using a given
function object)

and what I try to do:

int id = 1; // a report id
FindReportByID findByID(id); // my function object
FReport* pReport = findReport(findByName); // ???!!!! error here C2064:
term does not evaluate to a function
What am I doing wrong?


You're not providing a definition of 'findByName'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 24 '06 #3
CS
Sorry, it shoud be
FReport* pReport = findReport(findByID); // ???!!!! error here C2064:
term does not evaluate to a function

Mar 24 '06 #4
CS
Sorry, I am talking about passing STL function objects as arguments,
not ordinary functions.

Mar 24 '06 #5
CS wrote:

and the function to find a report using a function object:

FReport* findReport(std::unary_function<FReport*,bool>&
rFunctionObject) const


unary_function has no virtual functions, just a couple of typedefs.
There's nothing useful you can do with a reference to it. Instead, make
your function a template that takes its function object by value:

template <class Pred>
FReport* findReport(Pred pr) const
{
const RepoortIt pos = std::find_if(
m_reports.begin(), m_reports.end(), pr);
if (pos != m_reports.end())
return *pos;
else
return ? --- not found
}

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 24 '06 #6
CS
Yes, that works, thanks.

Mar 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double...
3
by: shahehe | last post by:
Hi I have a list of strings in a file and I want to call different functions according to each string I parsed from the file. For example, if I read "LOAD" from the file, I should call void...
8
by: chook | last post by:
How i can call my function with some parameters, if i have a pointer to this function
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
7
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
18
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.