gpt4 book ai didi

c++ - 用仿函数错误排序

转载 作者:行者123 更新时间:2023-11-30 04:23:53 24 4
gpt4 key购买 nike

想知道是否有人可以帮助我解决这个问题。下面的代码给我这个错误: fatal error C1903:无法从以前的错误中恢复;停止编译

template <class T>
class CompareList
{
public:

CompareList( const long& lBlobFeature, const bool& bIsAscending )
{
...
}


bool operator()( T &lhs, T &rhs )
{

double dFirstValue = lhs.GetValue( ... );
double dSecondValue = rhs.GetValue( ... );


if( m_bIsAscending ) // Sort Ascending.
{
if( dFirstValue < dSecondValue )
return true;
else
return false;
}
else // Sort Descending.
{
if( dFirstValue > dSecondValue )
return true;
else
return false;
}
}

};


CVParentList *m_pList;
m_pList = new CVChildList[ nBlobs ]; //CVChildList is a derived class of CVParentList

std::sort( m_pList, m_pList+GetBlobsNumber(), CompareList <CVChildList> ( lBlobFeature, TRUE) );

编辑:真的很抱歉,其实这是第一个错误:错误 C2664:“bool CompareList::operator ()(T &,T &)”:无法将参数 1 从“CVParentList”转换为“CVChildList &”

“ fatal error C1903:无法从以前的错误中恢复;停止编译”来了之后,我只看到了最后一条错误消息。非常抱歉。

最佳答案

您的比较器,或您的动态列表。需要改变。您可以扔掉比较器的模板部分,只需将其声明为 CVParentList 比较器:

class CompareList
{
public:
CompareList(long lBlobFeature, bool isAscending);

bool operator()(const CVParentList& left, const CVParentList& right) const
{
bool ans = false;
// your comparison code goes here
return ans;
}
private:
bool m_bIsAscending;
};

并调用您的 std::sort<>,就像您在没有模板参数的情况下那样

std::sort( m_pList, m_pList+GetBlobsNumber(), CompareList( lBlobFeature, TRUE) );

您还可以分配列表,对其进行排序,然后在完成后向下转换列表头:

CVParentList *m_pList = new CVChildList[ nBlobs ];
std::sort( (CVChildList *)m_pList, (CVChildList *)m_pList+GetBlobsNumber(), CompareList<CVChildList> ( lBlobFeature, TRUE) );

但我真的建议您选择第一个选项。

关于c++ - 用仿函数错误排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13135298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com