error: no matching function for call to

来源:互联网 发布:国家燃烧知乎 编辑:程序博客网 时间:2024/05/01 18:45
原文地址:error: no matching function for call to作者:brightmoon

今天用c++标准模板库(STL)中的sort算法给一个vector<pair<int,double>>对象排序时遇到了题目中的编译时错误:(问题虽然简单,却让我弄了整整一下午)

error: no matching function for call to'sort(__gnu_cxx::__normal_iterator<std::pair<int,double>*  ......

note: candidates are: void std::sort(_RandomAccessIterator,_RandomAccessIterator, _Compare) ......

 

程序是这样的:

fourdct.h

boolPhasePairLess(pair<int,double>p1,pair<int,double>p2);//这是fourdct类的成员函数。

 

fourdct.cpp

vector<pair<int,double>> phasePairVector;

....

sort(phasePairVector.begin(),phasePairVector.end(),PhasePairLess);

 

boolFourDCT::PhasePairLess(pair<int,double>p1,pair<int,double> p2)
{
 return p1.second <p2.second;
}

错误的原因是比较函数PhasePairLess应当是static的。将头文件中的函数声明改为:

static boolPhasePairLess(pair<int,double>p1,pair<int,double> p2);

编译通过。注意,cpp文件中不用static。

0 0