-1

i have problem using the sort algorithm with my list of objects. Here is my code

#include <iostream>
#include <string>
#include <list>
//#include <iterator>
//#include <functional>
#include <algorithm>
using namespace std;

class Project
{
private:
    string name;
    int days;
public:
    Project(string n, int d)
    {
        name = n;
        days = d;
    }
    int get_days() const
    {
        return days;
    }

    void show() const
    {
        cout << "Name of the project: " << name << endl;
        cout << "Days to completion: " << days << endl;
        cout << endl;
    }
};
static bool sortByAge(const Project &lhs, const Project &rhs) 
{ 
    return lhs.get_days() < rhs.get_days(); 
}
int main()
{
    list<Project> l1, l2;
    Project ob1("Alpha", 120), ob3("Gama", 60), ob5("Omega", 200);
    l1.push_back(ob1);
    l1.push_back(ob3);
    l1.push_back(ob5);
    sort(l1.begin(), l1.end(), sortByAge);
    cout << "LIST 1" << endl;
    for (const auto& p : l1)
    {
        p.show();
    }
system("pause");
}

And these are the errors

Error   4   error C2676: binary '-' : 'std::_List_iterator<std::_List_val<std::_List_simple_types<Project>>>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm    3157
Error   5   error C2780: 'void std::_Sort(_RanIt,_RanIt,_Diff,_Pr)' : expects 4 arguments - 3 provided  c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm    3157
Error   3   error C2784: 'unknown-type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Project>>>'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm    3157
Error   2   error C2784: 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Project>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm    3157
Error   1   error C2784: 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Project>>>'  c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm    3157
Dobri
  • 1
  • 1
  • Welcome to Stack Overflow! Please paste your code in your question, forming a [mcve], so it might be useful to other people long after hastebin has vanished from the Internet. – Jack Deeth Dec 05 '16 at 20:49
  • Please read ["How do I ask a good question?"](http://stackoverflow.com/help/how-to-ask) – Fred Larson Dec 05 '16 at 20:49
  • Welcome to SO. Please do not refer to your through links to other sites or with images. It is far more convenient for the readers of your post to see it in your post. Furthermore, please avoid of copy/paste the whole program. You should just refer to a few lines of codes, in which you have sticked. Last but not least visit the link that Fred mentioned above. It is very helpful. – Christos Dec 05 '16 at 20:49
  • Reopened. This was closed as a duplicate of a question about how to sort a **vector**. This question is about a **list**. – Pete Becker Dec 05 '16 at 20:51
  • @Christos: There is quite a good chance the question will be closed as off-topic if it does *not* contain a complete program - however the poster will need to try and minimize the program as much as possible. In this case the default constructor is not needed, the set_name, get_name, and set_days functions are not needed, ob2 and ob4 are not needed, and finally, the lines commenting them out are not needed. – Martin Bonner supports Monica Dec 05 '16 at 21:00

2 Answers2

1

The problem is that std::sort requires random access iterators, but std::list doesn't provide them; it only supports bidirectional iterators. That's why std::list has it's own sort member function. So instead of calling std::sort(l1.begin(), l1.end(), sortByAge) call l1.sort(sortByAge).

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

std::sort accepts RandomAccessIterators

template< class RandomIt >
void sort( RandomIt first, RandomIt last );

but std::list::iterator is a BidirectionalIterator.

Since you are sorting the entire list, you can use std::list::sort instead.

l1.sort(sortByAge);
milleniumbug
  • 15,379
  • 3
  • 47
  • 71