当前位置:网站首页>sort函数使用cmp出错Line 22: Char 38: error: reference to non-static member function must be called
sort函数使用cmp出错Line 22: Char 38: error: reference to non-static member function must be called
2022-07-30 08:07:00 【pied_piperG】
出错代码
class Solution {
public:
int math(int n)
{
int ans = 0;
while (n)
{
n &= (n - 1);
ans++;
}
return ans;
}
bool cmp(int x, int y)
{
if (math(x) == math(y))
return x < y;
else
return math(x) < math(y);
}
vector<int> sortByBits(vector<int>& arr)
{
sort(arr.begin(), arr.end(), cmp);
return arr;
}
};
解决方法:将其它成员函数(cmp函数)移动到public外,或者加上static
class Solution {
public:
int math(int n)
{
int ans = 0;
while (n)
{
n &= (n - 1);
ans++;
}
return ans;
}
static bool cmp(int x, int y)
{
if (math(x) == math(y))
return x < y;
else
return math(x) < math(y);
}
vector<int> sortByBits(vector<int>& arr)
{
sort(arr.begin(), arr.end(), cmp);
return arr;
}
};边栏推荐
猜你喜欢
随机推荐
npm指令
怎么在本地电脑上运行dist文件
ACL 2022 | 引入角度margin构建对比学习目标,增强文本语义判别能力
What convenience does the RFID fixed asset inventory system bring to enterprises?
【无标题】
剑指offer 48:最长不重复子串
opencv可以有多有趣
C语言经典练习题(3)——“汉诺塔(Hanoi)“
电源完整性的去耦和层间耦合电容
与tcp协议有关的几个知识点
leetcode经典问题——11.盛水最多的容器
2022杭电多校第一场
如何避免CMDB沦为数据孤岛?
解构的运用
SEER数据库中“手术变量(RX SUMM-SURG OTH REG/DIS )”下的字段解释
积分专题笔记-积分的定义
立创EDA——PCB的走线(五)
【HMS core】【FAQ】HMS Toolkit典型问题合集1
电路分析:运放和三极管组成的恒流源电路
js柯里化








