trouble with std::find_if()

N

Nick Keighley

Hi,

this is cut from a larger program.

<code>

#include <algorithm>
#include <vector>


typedef unsigned int SapId;

class Sap
{
public:
explicit Sap(SapId id): id_(id)
{}

SapId ident () const
{ return id_; }

private:
SapId id_;
};


typedef std::vector<Sap*> SapList;

SapList sap_list_;


class SapIdMatchPredicate
{
public:
SapIdMatchPredicate(SapId id): id_(id)
{}

bool operator() (const Sap& sap) const
{ return sap.ident() == id_; }

private:
const SapId id_;
};

SapList::iterator find (const SapId sap_id)
{
SapIdMatchPredicate sap_id_match (sap_id);
return std::find_if (sap_list_.begin(), sap_list_.end(),
sap_id_match);
}

int main (void)
{
SapList::iterator i;
i = find (1234); return 0;
}

</code>

I've presumably done something wrong with the functor. I get
compilation errors

'()' : cannot convert parameter 1 from 'class Sap *' to 'const
class Sap &'
term does not evaluate to a function


the erroneous line is deep in algorithm.h


--
Nick Keighley

The rabbit snarled and hefted his submachine gun angrily.
Ears back and teeth visible, he hissed at the cyborg.
Charles Stross "Singularity Sky"
 
N

Nick Keighley

On 2 Jul, 14:11, Nick Keighley <[email protected]>
wrote:

AhA!

I found the problem (see OP for details):-


typedef std::vector<Sap*> SapList; <--- Sap*

SapList sap_list_;

class SapIdMatchPredicate
{
public:
    SapIdMatchPredicate(SapId id): id_(id)
    {}

    bool operator() (const Sap& sap) const <--- Sap&
    {    return sap.ident() == id_;    }

private:
    const SapId id_;

};

<snip>

if it's a vector of Sap* I need to compare Sap* not Sap&
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top