By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Why this is wrong in calling bind1st

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 07 Aug 2004 21:14:11 -0600, John Black <bl***@eed.com> wrote:
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
Jul 22 '05 #2

P: n/a
John Harrison wrote:
On Sat, 07 Aug 2004 21:14:11 -0600, John Black <bl***@eed.com> wrote:
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


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?
Jul 22 '05 #3

P: n/a
On Sun, 08 Aug 2004 00:12:32 -0600, John Black <bl***@eed.com> wrote:
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.