Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Для новичков > PointerContainer


Автор: kapusta007 13.3.2019, 00:11
добрый вечер!
Начал изучать 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;
}

Автор: kapusta007 13.3.2019, 21:07
разобрался
Код

   sort(v.cbegin(), v.cend(),[](const animal & a1, const animal & a2)->bool {return (a1.legs < a2.legs); });


решение в следующем надо было использовать  обычные итераторы.
теперь работает все вот исправленная версия
Код

   sort(v.begin(), v.end(),[](const animal & a1, const animal & a2)->bool {return (a1.legs < a2.legs); });


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)