Witam.
Chciałbym wiedzieć:
- w jaki sposób poprawnie zaimplementować własną funkcję sortującą kontener set<shared_ptr<Person>>. W poniższym kodzie otrzymuję błąd: „bool Person::operator ()(const Person &, const Person &)”: nie można dokonać konwersji argumentu 1 z „const std::shared_ptr<Person>” do „const Person &”
- oraz w jaki sposób wypisać elementy takiego kontenera. Obecnie dostaję błąd: „Person::getAge”: niestandardowa składnia; użyj znaku „&”, aby utworzyć wskaźnik elementu członkowskiego
#include <iostream>
#include <string>
#include <memory>
#include <set>
using namespace std;
class Person {
int age;
string name;
public:
Person(int age = 0, string name = "") : age(age), name(name) {}
~Person() {}
bool operator()(const Person &first, const Person &second) {
return first.getAge() < second.getAge();
}
int getAge() const {
return age;
}
string getName() const {
return name;
}
};
int main() {
set<shared_ptr<Person>, Person> zbior;
zbior = {
shared_ptr<Person>(new Person(21, "Rambo")),
shared_ptr<Person>(new Person(27, "Ken")),
shared_ptr<Person>(new Person(18, "Merry")),
shared_ptr<Person>(new Person(29, "Elizabeth")),
shared_ptr<Person>(new Person(40, "John"))
};
for (const auto &elem : zbior)
cout << elem->getAge << ", " << elem->getName << endl;
cin.get();
}