error: no matching function for call to

本文介绍了一个关于使用C++ STL中的sort算法给vector<pair<int,double>>对象排序时遇到的编译错误,并详细解释了解决方案。

今天用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。

虽然给定引用中未直接提及“Char 23: error: no matching function for call to object of type”错误的解决方案,但可以从类似错误的解决思路中获得启发。 ### 常见原因及解决办法 #### 函数调用与定义不匹配 在代码里,若函数调用时的参数类型、数量和顺序与函数定义不一致,就会引发该错误。需要仔细检查函数调用处的参数,保证和函数定义相匹配。例如在信号槽连接时,若函数参数不匹配就会报错,像在“error: no matching function for call to ‘Dialog::connect(QComboBox*&, , Dialog::creatCtrlFrame()::<lambda()>)’”中,就需要检查连接函数的参数是否正确 [^3]。 #### 版本兼容性问题 若使用了不同版本的库或者框架,可能会出现函数调用不匹配的情况。例如在Qt信号槽使用中,connect采用Qt 5的语法却在QT4中运行,就会报错。此时可以尝试使用QT5之前的语法来解决,如使用`QObject::connect(comboBoxVersion, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(slotDisplayVersion(const QString &)));` [^4]。 #### 类型转换问题 在进行函数调用时,若参数类型不能隐式转换为函数定义所需的类型,也会出现此错误。要确保参数类型和函数定义的类型一致,必要时进行显式类型转换。例如在“ No matching function for callto ‘iconv_close‘ No matching function for call to ‘iconv’ ”问题中,通过修改类型转换格式解决了问题,将`iconv(_iconv, (char**)&pin, &inLen, &pout, &outLen);`改为`iconv((iconv_t)_iconv, (char**)&pin, &inLen, &pout, &outLen);` [^5]。 #### 类继承问题 在使用某些框架(如Qt)时,若类没有继承特定的基类,可能会导致函数调用错误。比如在QT中使用信号槽时,若类没有继承QObject类,就会报错,需要确保类正确继承相关基类 [^2]。 ### 示例代码检查 ```cpp #include <iostream> // 函数定义 void myFunction(int num) { std::cout << "The number is: " << num << std::endl; } int main() { // 错误的函数调用,参数类型不匹配 // myFunction("hello"); // 这会导致 no matching function for call 错误 // 正确的函数调用 myFunction(42); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值