473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

anything wrong calling bind2nd this way?

Hi,
I have some code like this,

itr = find_if(this->pool.begin(), this->pool.end(),
bind2nd(mem_fun_ref(&Pool::isAvailable),
make_pair(base, high)));

But compiler always complains a lot STL template instantiation
errors.
My class definition is like this,

class Pool{
public:
vector<pair<int, int> > pool;

bool isAvailable(pair<int, int>);

void func();
};

and the above find_if is a statement with Pool::func().
Thanks.

Jul 22 '05 #1
3 1920
On Tue, 01 Jun 2004 19:27:27 -0600, John Black <bl***@eed.com> wrote:
Hi,
I have some code like this,

itr = find_if(this->pool.begin(), this->pool.end(),
bind2nd(mem_fun_ref(&Pool::isAvailable),
make_pair(base, high)));

But compiler always complains a lot STL template instantiation
errors.
My class definition is like this,

class Pool{
public:
vector<pair<int, int> > pool;

bool isAvailable(pair<int, int>);

void func();
};

and the above find_if is a statement with Pool::func().


Okay, this one took me /way/ too long to figure out. It is one of those
optical-illusion types of errors ;-)

Think about what you're invoking your call to find_if upon: a vector of
pairs. NOT a vector of "Pool" objects. Yet you're setting the find_if call
up as if each element it'll be iterating over is a Pool (i.e., you're
trying to have the algorithm invoke a member function on each element of
the vector.) But the vector's elements aren't Pools, they're std::pairs.
What you probably wanted was something as simple as this:

itr = find(pool.begin(), pool.end(), make_pair(base, high));

HTH,
-leor
Thanks.


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:
On Tue, 01 Jun 2004 19:27:27 -0600, John Black <bl***@eed.com> wrote:
Hi,
I have some code like this,

itr = find_if(this->pool.begin(), this->pool.end(),
bind2nd(mem_fun_ref(&Pool::isAvailable),
make_pair(base, high)));

But compiler always complains a lot STL template instantiation
errors.
My class definition is like this,

class Pool{
public:
vector<pair<int, int> > pool;

bool isAvailable(pair<int, int>);

void func();
};

and the above find_if is a statement with Pool::func().


Okay, this one took me /way/ too long to figure out. It is one of those
optical-illusion types of errors ;-)

Think about what you're invoking your call to find_if upon: a vector of
pairs. NOT a vector of "Pool" objects. Yet you're setting the find_if call
up as if each element it'll be iterating over is a Pool (i.e., you're
trying to have the algorithm invoke a member function on each element of
the vector.) But the vector's elements aren't Pools, they're std::pairs.
What you probably wanted was something as simple as this:

itr = find(pool.begin(), pool.end(), make_pair(base, high));

HTH,
-leor
Thanks.


Leor,
Thanks for pointing the hierarchy error.

But the problem is I must use a function as search condition, actually the
reason is simple, this is how I implement isAvailable():

if (...this->pool has a pair which 'cover' the given pair... ) {
return true;
}

Here one pair 'cover' another means that this pair's first is less and
equals than the other's first and its second is large and equals to the
other's second.

Any suggestions?

Regards.

Jul 22 '05 #3
John Black <bl***@eed.com> wrote in message news:<40***************@eed.com>...
But the problem is I must use a function as search condition, actually the
reason is simple, this is how I implement isAvailable():

if (...this->pool has a pair which 'cover' the given pair... ) {
return true;
}

Here one pair 'cover' another means that this pair's first is less and
equals than the other's first and its second is large and equals to the
other's second.

Any suggestions?

Well .. it seems there may be two levels of iteration here ..
otherwise I don't understand why you need std::find_if. Your stated
implementation of isAvailable seems to be mimicking the operation of
std::find_if anyway.

So, if all you want to do is search a vector of pair<int,int> (inside
a Pool object) for the first instance satisfying your "cover"
conditions, then just write something like:

// Note that I have added std:: where you omitted it and also made
some
// necessary assumptions to answer your question. If my assumptions
are
// wrong, re-post with some more details and I will work from there

bool covers(const std::pair<int,int>& a, const std::pair<int,int>& b)
{
return a.first<=b.first && a.second>=b.second;
}

void func(Pool &pool, int base, int high) {
// I am assuming here that Pool has the appropriate typedef's and
forwarding
// functions for the following code to work

Pool::iterator itr = std::find_if(pool.begin(), pool.end(),
std::bind2nd(std::ptr_fun(covers),

std::make_pair(base,high)));

// rest of function using result
}

If there is another level of "finding" that needs to be done, you can
post some more details and I will try to help you more.

HTH, Dave Moore
Jul 22 '05 #4

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

Similar topics

1
by: red floyd | last post by:
Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call in the following code. Why can't I use bind2nd for a functor with a non-const reference as the second parameter? I have to...
3
by: ES Kim | last post by:
Here's a simple code: #include <vector> #include <algorithm> #include <functional> using namespace std; struct S { void f(int) const { }
2
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
4
by: dzikus | last post by:
Why the following code does not compile? If I change the function argument to value (not reference) everything is ok. Is it possible to modify such code that it will compile with reference argument...
8
by: Noah Roberts | last post by:
#include <vector> #include <algorithm> #include <functional> class X { int x; public: X(int i) : x(i) {} bool eq(const X & other) const { return x == other.x; }
2
by: benben | last post by:
I tried to do the following: bind2nd(ptr_fun(&tolower<char>), locale()); But was hit with errors claiming some where deep down the STL library some code is trying to take a reference to a...
2
by: benben | last post by:
I wrote the following lines: #include <locale> #include <functional> #include <algorithm> int main() { using namespace std; bind2nd(ptr_fun(tolower<char>), locale());
3
by: Bruintje Beer | last post by:
Hi, I am having the following question (see code below) the class Data is declared as class Data { public : // rest of class };
4
by: responsible | last post by:
Hi, Digging through the STL source code, i found this gem.. template <class _Operation, class _Tp> inline binder2nd<_Operation> bind2nd(const _Operation& __fn, const _Tp& __x) { typedef...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.