error: reference to non-static member function must be called sort(nums.begin(),nums.end(),cmp1)
生活随笔
收集整理的這篇文章主要介紹了
error: reference to non-static member function must be called sort(nums.begin(),nums.end(),cmp1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
leetcode179題中編譯出現則個問題
出錯的代碼:
class Solution { public:bool cmp1(const int& a, const int& b){string s1=to_string(a);string s2=to_string(b);string ans1=s1.append(s2);string ans2=s2.append(s1);return ans1>ans2;}string largestNumber(vector<int>& nums) {sort(nums.begin(),nums.end(),cmp1);string ans="";int len1=nums.size();if(nums[0]==0){return "0";}for(int i=0;i<len1;++i){ans.append(to_string(nums[i]));}return ans;} };我本來想自定義compare函數傳入sort中,但卻報錯。
error: reference to non-static member function must be called sort(nums.begin(),nums.end(),cmp1);
原因是我把cmp1函數寫在類里面,普通的成員函數都有一個隱含的this指針,表面上看cmp1只有兩個參數,但實際上它有三個參數,而調用sort()排序函數的時候只需要用到兩個參數進行比較,所以就出現了形參與實參不匹配的情況(函數有三個形參,但是只輸入了兩個實參)。
解決方法:
把cmp1移出類外
總結
以上是生活随笔為你收集整理的error: reference to non-static member function must be called sort(nums.begin(),nums.end(),cmp1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在linux下yum安装mysql_
- 下一篇: date js 半年_js Date 日