Why this is wrong in calling bind1st

J

John Black

I have the following code trying to use bind1st,

class C1{
...
};

class C2{
...
};

class util{
static bool isSame(C1*, C2*);
};


int main(){
vector<C2*> c2s;
C1* c1;

...

vector<C2*>::iterator itr = find(c2s.begin(), c2s.end(),
bind1st(mem_fun(util::isSame), c1);
);


But my compiler complains syntax error in calling bind1st, do you see
anything wrong?

Thanks.
 
J

John Harrison

I have the following code trying to use bind1st,

class C1{
...
};

class C2{
...
};

class util{
static bool isSame(C1*, C2*);
};


int main(){
vector<C2*> c2s;
C1* c1;

...

vector<C2*>::iterator itr = find(c2s.begin(), c2s.end(),
bind1st(mem_fun(util::isSame), c1);
);


But my compiler complains syntax error in calling bind1st, do you see
anything wrong?

Thanks.


Yes lots.

You have a spurious semi colon (or mismatched parens).

You are missing a & before util::isSame.

mem_fun only works on member functions, not static member functions.

mem_fun only works on member functions with zero or one arguments.

There is no version of find that takes a predicate, perhaps you meant
find_if.

The following compiles, is this what you meant?

#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

class C2{
};

class C1{
public:
bool isSame(C2*) { return false; }
};

int main(){
vector<C2*> c2s;
C1* c1;
vector<C2*>::iterator itr = find_if(c2s.begin(), c2s.end(),
bind1st(mem_fun(&C1::isSame), c1)
);
}

john
 
J

John Black

John said:
Yes lots.

You have a spurious semi colon (or mismatched parens).

You are missing a & before util::isSame.

mem_fun only works on member functions, not static member functions.

mem_fun only works on member functions with zero or one arguments.

There is no version of find that takes a predicate, perhaps you meant
find_if.

The following compiles, is this what you meant?

#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

class C2{
};

class C1{
public:
bool isSame(C2*) { return false; }
};

int main(){
vector<C2*> c2s;
C1* c1;
vector<C2*>::iterator itr = find_if(c2s.begin(), c2s.end(),
bind1st(mem_fun(&C1::isSame), c1)
);
}

john

Thanks for your pointing, actually before your post I found that I should
not use mem_fun because my binary_predictable function is static, and in
my code, it must be a static function, so I changed bind1st like this,

vector<C2*>::iterator itr = find_if(c2s.begin(), c2s.end(),
bind1st(ptr_fun(util::isSame), c1);

Is there any wrong here?
 
J

John Harrison

John Harrison wrote:


Thanks for your pointing, actually before your post I found that I should
not use mem_fun because my binary_predictable function is static, and in
my code, it must be a static function, so I changed bind1st like this,

vector<C2*>::iterator itr = find_if(c2s.begin(), c2s.end(),
bind1st(ptr_fun(util::isSame), c1);

Is there any wrong here?

Apart from the missing ) is looks OK to me.

john
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top