我有问题使用排序算法与我的列表对象。这里是我的代码排序列表的对象,使用排序()算法c + +
#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");
}
而这些都是错误
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
欢迎来到Stack Overflow!请将你的代码粘贴到你的问题中,形成一个[mcve],所以在hastebin从互联网上消失很久以后,它可能对其他人有用。 –
请阅读[“我如何问一个好问题?”](http://stackoverflow.com/help/how-to-ask) –
欢迎来到SO。请不要将您的链接指向其他网站或图片。您的帖子的读者在您的帖子中看到它会更方便。此外,请避免复制/粘贴整个程序。你只需要参考几行你已经粘贴的代码。最后但并非最不重要的访问弗雷德上面提到的链接。这非常有帮助。 – Christos