473,951 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ 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::isA vailable),
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(pai r<int, int>);

void func();
};

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

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

itr = find_if(this->pool.begin() , this->pool.end(),
bind2nd(mem_fun _ref(&Pool::isA vailable),
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(pai r<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.co m> wrote:
Hi,
I have some code like this,

itr = find_if(this->pool.begin() , this->pool.end(),
bind2nd(mem_fun _ref(&Pool::isA vailable),
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(pai r<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.co m> wrote in message news:<40******* ********@eed.co m>...
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,i nt>& a, const std::pair<int,i nt>& b)
{
return a.first<=b.firs t && a.second>=b.sec ond;
}

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(po ol.begin(), pool.end(),
std::bind2nd(st d::ptr_fun(cove rs),

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
3394
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 use kludge it by using something like KludgeFunctor(). --------------------------- #include <vector> #include <algorithm> #include <functional>
3
2684
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
1532
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
4928
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 type? Best regards Dominik #include <string> #include <list> #include <algorithm>
8
4513
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
1925
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 reference type. I guess that is because the std::tolower<>() template takes a reference to a locale as its second parameter then bind2nd tries to take the reference of that parameter. But how to I circumvent this?
2
483
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
1831
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
1857
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 typename _Operation::second_argument_type _Arg2_type; return binder2nd<_Operation>(__fn, _Arg2_type(__x)); }
0
10174
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9997
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10704
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8271
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7447
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6237
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.