C++复习笔记--error: static assertion failed: comparison object must be invocable as const的解决

1--报错代码

class Mycompare{
public:
    bool operator()(const int &v1, const int &v2) {
        return v1 > v2; // 实现从大到小排序
    }
};

2--解决方法

class Mycompare{
public:
    bool operator()(const int &v1, const int &v2) const {
        return v1 > v2; // 实现从大到小排序
    }
};

3--原因分析

        C++17 标准规定 comparison object must be invocable as const,则参数列表必须加 const 修饰!

猜你喜欢

转载自blog.csdn.net/weixin_43863869/article/details/129740588
今日推荐