我正在尝试将用户定义的类'A'与模板std::less一起使用。我还具有<
所需的重写std::less
运算符的功能。此代码未编译。
#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/