当前位置:网站首页>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;
}
};
边栏推荐
- The sword refers to offer 48: the longest non-repeating substring
- 研发转至FAE(现场应用工程师),是否远离技术了?有前途吗?
- 香港服务器iis配置web服务器如何操作?
- test4
- 【HMS core】【FAQ】HMS Toolkit典型问题合集1
- It is said that FPGA is high-end, what can it do?
- JS中对事件流的理解
- 积分专题笔记-与路径无关条件
- Webview中的超链接点击到外部浏览器打开
- Circuit analysis: constant current source circuit composed of op amp and triode
猜你喜欢
一文带你玩转offer-01
C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
leetcode经典问题——11.盛水最多的容器
【SQL server速成之路】——身份验证及建立和管理用户账户
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
HashSet and LinkedHashSet
剖析SGI STL空间配置器(_S_refill内存块填充函数)
How to Assemble a Registry
如何组装一个注册中心
hcip实验
随机推荐
一文带你玩转offer-01
Splunk tag 的利用场景
积分简明笔记-第一类曲线积分的类型
编程界的“躲猫猫”比赛 | 每日趣闻
The difference between typescript3-ts and js
解构的运用
SwiftUI SQLite 教程之 构建App本地数据库实现创建、读取、更新和删除(教程含完成项目源码)
【零基础玩转BLDC系列】以GD32F30x为例定时器相关功能详解
What convenience does the RFID fixed asset inventory system bring to enterprises?
function (1)
leetcode经典问题——11.盛水最多的容器
Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
Activating data potential Amazon cloud technology reshapes cloud storage "family bucket"
用示波器揭示以太网传输机制
test4
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
电路分析:运放和三极管组成的恒流源电路
Leetcode - 990: equations of satisfiability
最远点采样 — D-FPS与F-FPS
Lenovo Notebook How to Change Windows 10 Boot Logo Icon