我有問題使用排序算法與我的列表對象。這裏是我的代碼排序列表的對象,使用排序()算法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