function pointers as arguments

C

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.
 
A

Alf P. Steinbach

* 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'.
 
C

CS

Sorry, it shoud be
FReport* pReport = findReport(findByID); // ???!!!! error here C2064:
term does not evaluate to a function
 
C

CS

Sorry, I am talking about passing STL function objects as arguments,
not ordinary functions.
 
P

Pete Becker

CS said:
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
}
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top