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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with boolean operator

Status
Not open for further replies.

johndw

Technical User
Nov 15, 2001
8
US
// Can anyone help solve this ??? I tried to use this example as the basis for training... however ...
// I receive the following errors at complie time :
/*
--------------------Configuration: person set 1 - Win32 Debug--------------------
Compiling...
person set 1.cpp
C:\j\hwc2\New Folder\person set 1.cpp(34) : error C2804: binary 'operator <' has too many parameters
C:\j\hwc2\New Folder\person set 1.cpp(35) : error C2804: binary 'operator ==' has too many parameters
Error executing cl.exe.
*/
// Could you help me to resolve this problem. ??? Please ???


#include <set>
#include <conio.h> // for using getch()
#include <string>
// #include <algorithm>
#include <iostream>
//#include <stdlib.h>

using namespace std;
//-------------------- using a multiset
class person
{
protected:
string Lname;
string Fname;

public:
person(): Lname(&quot;null&quot;), Fname(&quot;null&quot;) {} //default ctr

person(string Ln, string Fn): Lname (Ln), Fname (Fn) {} //2 arg ctr

bool operator < (const person&, const person&);
bool operator == (const person&, const person&);

void display() const //output person data
{ cout << endl << Lname << &quot;, \t&quot; << Fname << &quot;, \t&quot; << endl; }

};

bool person::eek:perator < (const person& p1, const person& p2) //operator for < comparison
{
if(p1.Lname == p2.Lname)
{ return(p1.Fname < p2.Fname) ? true : false; }
return(p1.Lname < p2.Lname) ? true : false;
}

bool person::eek:perator == (const person& p1, const person& p2) //operator for == comparison
{
return(p1.Lname == p2.Lname && p1.Fname == p2.Fname) ? true : false;
}

//---------------------Now to make it happen. -------------
int main()
{
person p1(&quot;Smurf&quot;, &quot;Popa&quot;);
person p2(&quot;Jojojo&quot;, &quot;Mojo&quot;);
person p3(&quot;Willis&quot;, &quot;John&quot;);
person p4(&quot;Willis&quot;, &quot;Jolene&quot;);
person p5(&quot;Snow&quot;, &quot;Benjamin&quot;);
person p6(&quot;MacDonald&quot;, &quot;Ronald&quot;);
person p7(&quot;Silver&quot;, &quot;LongJohn&quot;);
//created lots of instances to play with.

multiset<person, less<person> > persSet; //iterator to a multiset of persons -- I think ?
multiset<person, less<person> > ::iterator iter;

//put the people into the multiset
persSet.insert(p1);
persSet.insert(p2);
persSet.insert(p3);
persSet.insert(p4);
persSet.insert(p5);
persSet.insert(p6);
persSet.insert(p7);

cout << &quot;\nNumber of entries = &quot; << persSet.size();

iter = persSet.begin();
while (iter != persSet.end() )
(*iter++).display();

string searchLname, searchFname;
cout << &quot;\n\nEnter last name of person to search for : &quot;;
cin >>searchLname;
cout << &quot;\nEnter first name : &quot;;
cin >> searchFname;

//must create such a person's name to use in search
person searchPerson(searchLname, searchFname);


//count the number of occurances
int cntPersons = persSet.count(searchPerson);
cout << &quot;The number of people with that name is : &quot; << cntPersons;

//show all the names that match
iter = persSet.lower_bound(searchPerson);
while(iter != persSet.upper_bound(searchPerson))
(*iter++).display();
cout << endl;


getch ();
return 0;
}
 
bool person::eek:perator < (const person& p1, const person& p2)

should be

bool person::eek:perator < (const person& p1)
{
if(p1.Lname == p2.Lname)
{
return(p1.Fname < Fname) ? true : false;
}
return(p1.Lname < Lname) ? true : false;
}
 
sorry... submitted too early... still on my first cup o coffee. Follow this for all overloaded operators. You are comparing it with the current class so you dont need to have 2 parameters. Only one.

Matt
 
Thanks for the idea Matt.
I tried it but now I receive a different Microsoft error:
&quot;microsoft visual studio\vc98\include\functional(86) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class person' (or there is no acceptable conversion)&quot; .
I'm still learning so maybe I compromised it some other way. (Like the way I applied it ?)Were you able to run the program with that change ???
I &quot;//&quot; commented out the other operator to narrow down the errors to just that one. I also received 4 warnings which I believe are related to the error.
Hope you can help.
-John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top