Reputation: 77
Here is my class declaration:
class Person
{
private:
string name;
public:
void showData()
{
cout << name << endl;
}
void insertData()
{
cin >> name;
}
bool operator<(Person p)
{
return name < p.name;
}
};
Now I am trying to create a multiset of objects of person class, How to do so? Here is the main function which I to wrote:
int main()
{
multiset<Person> m;
for (int i = 0; i < 6; i++)
{
Person p;
p.insertData();
m.insert(p);
}
multiset<Person>::iterator it;
// for (it = m.begin(); it != m.end(); it++)
// {
// cout << it.name << endl;
// }
}
Upvotes: 0
Views: 213
Reputation: 87959
You need to define your operator<
in a slightly different way. I would define it as a friend not a class member, but if it is a class member it should be const
.
So this is OK
class Person
{
...
bool operator<(Person p) const
{
return name < p.name;
}
};
but I would do it like this
class Person
{
...
friend bool operator<(const Person& x, const Person& y)
{
return x.name < y.name;
}
};
Upvotes: 2