[c++]no matching function for call to ‘sort(…)......

【问题】

在做LeetCode的Merge Intervals时用到c++的sort函数,一直出这个错误,甚是郁闷。最后终于找到了问题所在。

【代码】

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        int count = intervals.size();
        if(count <= 1){
            return intervals;
        }//if
        // x轴排序
        sort(intervals.begin(),intervals.end(),cmp);
    }
private:
    // 比较函数
    bool cmp(Interval& ina,Interval& inb){
        return ina.start < inb.start;
    }
};

【解决】

cmp函数要定义在类的外面, 或者定义成为static类型

具体:

cmp()

{...}

class Solution

{

...

sort(...);

}

原因:

cmp是定义在一个类中的成员函数。开始的时候老是报错:no matching function for call to ‘sort(...), <unresolved overloaded function type>‘。后来我把该成员定义成静态函数,问题解决。原因可能和类成员函数的函数原型有关。所有成员函数都有一个隐含的指针参数,即this。这就和sort需要的comp函数原型不一致了,所以就报错了。而static函数就没有这个隐含参数了

如果类中有个cmp函数,调用时,类型就多了个this指针, 当然类型不对

时间: 2024-08-27 04:14:04

[c++]no matching function for call to ‘sort(…)......的相关文章

C++异常:no matching function for call to "Matrix(Matrix&amp;)"

C++异常:no matching function for call to "Matrix(Matrix&)" 我定义了一个类叫Matrix,其中构造函数explicit Matrix(const Matrix& source); 也写了一个方法: Matrix Matrix::myFun(const Matrix &source){  ...    return *this;} 编译报出上面的异常来,原因是explicit关键字抑制隐式转换,当我返回*thi

error: no matching function for call to &#39;std::exception:exception(const char[16])&#39;

环境:codeblocks 语言:C++ 在执行:throw new exception("queue is empty.");时 遇到问题:error: no matching function for call to 'std::exception:exception(const char[16])' 解决办法:修改为 std::logic_error e("xxx."); throw std::exception(e); error: no matching

no matching function transform?

http://stackoverflow.com/questions/19876746/stdtolower-and-visual-studio-2013 http://forums.codeguru.com/showthread.php?489969-no-matching-function-transform std::tolower is overloaded in C++, it's declared in <cctype> as int tolower(int); and also

no matching function for call to ‘std::basic_string&lt;char&gt;::assign(std::string&amp;, int)

使用string中的assign赋值函数报错,代码为: text0.assign(line,i+1); 其中text0与line都为string类型 最后发现assign函数的原型为 string &assign(const char *s,int n); 将代码改为以下即可 text0.assign(line.c_str(),i+1); 附  assign函数 string &operator=(const string &s);//把字符串s赋给当前字符串 string &am

Javascript 中 Array的 sort()和 compare()方法

Javascript 中 Array的 sort()方法其实是把要排序的内容转化为string(调用 toString()), 然后按照字符串的第一位 ascii 码先后顺序进行比较,不是数字. 我们看看官方是怎么说的: arrayobj.sort(sortfunction) 参数 arrayObj 必选项.任意 Array 对象. sortFunction 可选项.是用来确定元素顺序的函数的名称.如果这个参数被省略,那么元素将按照 ASCII 字符顺序进行升序排列. 说明 sort 方法将 A

基数排序(radix sort)

1 #include<iostream> 2 #include<ctime> 3 #include <stdio.h> 4 #include<cstring> 5 #include<cstdlib> 6 #include <map> 7 #include <string> 8 using namespace std; 9 // A utility function to get maximum value in arr[]

std::sort的详细用法

1 #include <algorithm> 2 #include <functional> 3 #include <array> 4 #include <iostream> 5 6 int main() 7 { 8 std::array<int, 10> s = { 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 }; 9 10 // sort using the default operator< 11 std::sort(s

快速排序(Quick Sort)

快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边).在这个分区结束之后,该基准就处于数列的中间位置.这个称为分区(partition)操作. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序. 下面这幅图会帮助你理解.选中的pivot用蓝色表示:

stream_iterator、ostream_iterator 使用初探

STL定义了供输入及输出的iostream iterator类,称为 istream_iterator和ostream_iterator,分别支持单一型别的元素的读取和写入. 使用方法: 1.包含头文件: #include <iterator>  using namespace std;       2.像使用其他iterator一样使用istream_iterator和 ostream_iterator.如:  使用一对"连接至标准输入"的iterator用于标示元素范围