Algorithm库 sort函数 cmp函数报错 unresolved overloaded funciton type

在C++编程中遇到<unresolved overloaded function type>错误,原因是cmp函数非静态导致。报错代码显示std::sort找不到匹配的cmp函数。解决方法是将cmp方法声明为static,因为非静态方法会隐含一个this指针,而sort不会传递这个额外参数。通过将cmp函数改为static类型,问题得到解决。尽管网上的其他方法可能在某些情况下有效,但在作者的程序中,只有将cmp设为static才能避免报错。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<unresolved overloaded funciton type>中文意思是:<未解析的重载函数类型>

我放一部分我的报错代码:

bool cmp(const Edge<TV, TE> &a, const Edge<TV, TE> &b) {
        return(a.weight < b.weight);
}
std::sort(edgeArr, edgeArr + edgeNum, cmp);

而且,上面那些都是在一个类里(注意:这里是报错的重点)

template<class TV, class TE> class AdjListGraph {...}

编译后就会有这样的报错:

[Error] no matching function for call to 'sort(Edge<int, int>*&, 
Edge<int, int>*, <unresolved overloaded function type>)'

void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = 
Edge<int, int>*; _Compare = bool (AdjListGraph<int, int>::*)
(const Edge<int, int>&, const Edge<int, int>&)]

no known conversion for argument 3 from 
'<unresolved overloaded function ty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值