首页 > 解决方案 > 当我尝试在 C++ 中使用 lamda 表达式进行排序时出现问题

问题描述

我必须按发现日期订购 Artefact 并打印内容。

我有这门课:

class Artifact{
protected:
    int discoveryDate_;
    int estimatedDateOfOrigin_;
    Artifact::artifactType _artifactType;
    Artifact::condition_ _condition;
};

我还有2个派生类GreekArtifact和Egypteam artifact,我创建了GreekArtifact的5个对象和Egypteam artifact的5个对象,并将它们添加到列表中

std::list<Artifact> artifacts;

我试图按发现日期对这个列表进行排序,我这样做了:

 std::sort(artifacts.begin(),artifacts.end(),[](auto &a, auto &b){
        return a->getDiscoveryDate() > b->getDiscoveryDate();
   });

而且这种方法不起作用,我不知道为什么,我真的不明白听起来像这样的错误:

error: no match for 'operator-' (operand types are 'std::_List_iterator<Artifact>' and 'std::_List_iterator<Artifact>')
     std::__lg(__last - __first) * 2,

你能帮我弄清楚我做错了什么吗?

标签: c++sorting

解决方案


推荐阅读