добрый вечер! Начал изучать boost по https://theboostcpplibraries.com/ в разделе PointerContainer (https://theboostcpplibraries.com/boost.pointer_container) в конце задача: создать множество классов animal и вставить их в контейнеры и потом одно полю отсортировать. при сортировке| Код | sort () или boost::indirect_fun<std::less<>> |
выдает ошибку не могу понять как ее решить?
| Код | Ошибка C2678 бинарный "=": не найден оператор, принимающий левый операнд типа "T" (или приемлемое преобразование отсутствует)
|
вот код.
| Код | #include <boost/ptr_container/ptr_vector.hpp> #include <boost/ptr_container/indirect_fun.hpp> #include <boost/ptr_container/ptr_inserter.hpp> #include <string> #include <array> #include <algorithm> #include <memory> #include <functional> #include <iostream> #include <sstream>
using namespace std;
class animal { public: animal() :legs(0), has_tail(0), name("") { ostringstream stream; stream << legs << ", " << has_tail << ", "; outstring = stream.str(); outstring += name; } animal(int leg, int tail, std::string n):legs(leg), has_tail(tail), name(n) { ostringstream stream; stream << legs << ", " << has_tail << ", "; outstring = stream.str(); outstring += name; } operator const char*()const {return outstring.c_str();} int legs; int has_tail; std::string name; string outstring; };
bool sort_pri(const animal & a1, const animal & a2) { return (a2.legs < a1.legs); }
int main() { //boost::ptr_vector<animal, boost::inderect_fun<std::less<animal>> v; boost::ptr_vector<animal> v; v.push_back({ new animal(2,1,"b") }); v.push_back({ new animal(5,1,"f") }); v.push_back({ new animal(3,1,"c") }); v.push_back({ new animal(1,1,"A") }); v.push_back({ new animal(4,1,"d") }); sort(v.cbegin(), v.cend(),[](const animal & a1, const animal & a2)->bool {return (a1.legs < a2.legs); }); for (auto elem = v.cbegin(); elem != v.cend(); ++elem) cout << elem->outstring << endl; return 0; }
|
|