c++ - std::less 编译问题

标签 c++ templates

我正在尝试将用户定义的类“A”与模板 std::less 一起使用。我还有一个覆盖 < 的函数std::less 要求的运算符(operator).此代码未编译。

#include<iostream>
#include<functional>
using namespace std;
class A{
public:
        A(int x=0):a(x){}
        int a;
        bool operator<(const A& ref){
                return a<ref.a;
        }
};
int main()
{
        A a1(1);
        A a2(2);
        std::less<A> comp;
        if( comp(a1,a2)){
                cout<<"less"<<endl;
        }
        else{
                cout<<"more"<<endl;
        }

}

最佳答案

成功了

    bool operator<(const A& ref) const{
            return a<ref.a;
    }

关于c++ - std::less 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062767/

相关文章:

c++ - 如何在 std::vector 中查找模式

c++ - 模板类的 dynamic_cast 编译错误

接口(interface) ptr 的 C++ 返回 vector

C++ Placement 新模板

c++ - 在成员函数中调用类构造函数

C++ shared_ptr std::bind 和 std::function

c++ - 极度 CPU 密集型闹钟

c++ - 为什么调用复制构造函数而不是转换构造函数?

c++ - 模板引用类型推导

templates - 确定模板的父页面