我正在尝试将用户定义的类“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/