leetcode的1356. 根据数字二进制下 1 的数目排序中,在类内部定义了一个compare,然后sort函数中使用了compare指针进行排序,结果出现了错误:
reference to non-static member function must be called:
这是因为对于非静态函数,都有一个隐含的参数this指针,即compare的声明实际上是bool compare(Solution* this, int a, int b)
,而sort函数要求compare有两个参数。
所以应该将compare声明成静态成员函数,静态成员函数没有隐含的this指针。
实际上,使用lambda是更好的选择。