[英]Declaration of std::sort functor
这是我的源代码的外观(简化):
class UncertaintyTest
{
private:
class SpectralPeak
{
};
std::list<SpectralPeak*> peaks;
};
struct SpectralPeakComparator
{
bool operator()(const UncertaintyTest::SpectralPeak &a, const UncertaintyTest::SpectralPeak &b)
{
return a.TMiddleAvg() < b.TMiddleAvg();
}
};
void UncertaintyTest::SortSpectralPeaks()
{
std::sort(peaks.begin(), peaks.end(), SpectralPeakComparator());
}
编译器拒绝编译此源代码,并出现以下错误:
C2676: binary '-' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2780: 'void std::_Sort(_RanIt,_RanIt,_Diff)' : expects 3 arguments - 4 provided c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const _Ty &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const std::complex<_Other> &,const _Ty &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const std::complex<_Other> &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
有问题的行是std:sort
。 如果我发表评论,一切都很好。 我对比较器对象的声明有什么问题?
问题是双重的:
首先,排序函子不接受正确的值类型。 您的列表包含指针,而不是直接包含对象,因此比较函子也必须使用指针:
struct SpectralPeakComparator
{
bool operator()(const UncertaintyTest::SpectralPeak *a, const UncertaintyTest::SpectralPeak *b)
{
return a->TMiddleAvg() < b->TMiddleAvg();
}
};
其次, std::list
没有std::sort
需要的随机访问迭代器。 您可以改为使用std::list::sort
:
peaks.sort(SpectralPeakComparator());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.