Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with bind2nd with user defined class

Status
Not open for further replies.

sean34

Programmer
Jan 17, 2004
2
0
0
SE
Hello

Im having trouble with bind2nd with a "user defined" class.
First I tried with integers using less<> and it works alright.
But when I would like it to work with my Person class, the complier (Borland Builder 5) is
giving me a hard time.

So this is my main.cpp:

struct MyLess
{
bool operator()(Person &first, int &n)
{
return (first.getAge() < n);
}
};
MyLess myLess;


int main()
{
vector<Person> v;
Person p;
for (int i = 0; i < 15; i++)
{
Person p(&quot;Sean&quot;, i);
v.push_back(p);
}
vector<Person>::iterator iter = v.begin();
//iter = partition(v.begin(), v.end(), bind2nd(myLess<p>(), 10));
//iter = partition(v.begin(), v.end(), bind2nd(MyLess<p>(), 10) );
//iter = partition(v.begin(), v.end(), bind2nd(myLess(), 10) );
//iter = partition(v.begin(), v.end(), bind2nd(MyLess(), 10) );

(Every object will of course be called &quot;Sean&quot;, but they have different &quot;age&quot;.)

Each and every one of these calls to partition() gives me error which I dont understand.
The first one:
[C++ Error] main.cpp(30): E2094 'operator<' not implemented in type 'MyLess' for arguments

of type 'Person'

Do I have to implement a &quot;op<&quot; in &quot;MyLess&quot;?


And my operator< which is defined as friend in Person.cpp:

bool operator<(Person &p1, Person &p2)
{
return (p1.getAge() < p2.getAge());
}


I guess there is something wrong with my functor.

Any Ideas?

/ Sean
 
From what I understand about this code fragment; the object MyLess (struct is really a public class) doesn't know what a 'Person' is and any comparison operations between MyLess and Person types need to be defined and known to MyLess.


Wisdom doesn't always come with age. Sometimes age comes alone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top