Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';具有自定义比较器功能的t排序列表_C++_List_Sorting - Fatal编程技术网

C++ Can';具有自定义比较器功能的t排序列表

C++ Can';具有自定义比较器功能的t排序列表,c++,list,sorting,C++,List,Sorting,当我尝试使用自定义比较器对成分进行排序时,我遇到了这个编译器错误 kitchen.cpp: In member function ‘void Kitchen::printContents(std::ofstream&)’: kitchen.cpp:172: error: no matching function for call to ‘std::list<Ingredient, std::allocator<Ingredient> >::sort(<unr

当我尝试使用自定义比较器对成分进行排序时,我遇到了这个编译器错误

kitchen.cpp: In member function ‘void Kitchen::printContents(std::ofstream&)’:
kitchen.cpp:172: error: no matching function for call to ‘std::list<Ingredient, std::allocator<Ingredient> >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/list.tcc:271: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Ingredient, _Alloc = std::allocator<Ingredient>]
/usr/include/c++/4.2.1/bits/list.tcc:348: note:                 void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (Kitchen::*)(const Ingredient&, const Ingredient&), _Tp = Ingredient, _Alloc = std::allocator<Ingredient>]
kitchen.cpp:在成员函数“void kitchen::printContents(std::of stream&)”中:
kitchen.cpp:172:错误:调用'std::list::sort()'时没有匹配的函数
/usr/include/c++/4.2.1/bits/list.tcc:271:注:候选项为:void std::list::sort()[带_Tp=component,_Alloc=std::allocator]
/usr/include/c++/4.2.1/bits/list.tcc:348:注:void std::list::sort(_StrictWeakOrdering)[带_StrictWeakOrdering=bool(Kitchen::*)(常量成分和常量成分和常量成分),常量成分和常量成分和常量成分,_Tp=成分,_Alloc=std::分配器]
以下是导致该问题的代码:

bool sortFunction(const Ingredient a, const Ingredient b)
{
    if (a.getQuantity() < b.getQuantity())
        return true;
    else if (a.getQuantity() == b.getQuantity())
    {
        if (a.getName() < b.getName()) return true;
        else return false;
    }
    else return false;
}

void Kitchen::printContents(std::ofstream &ostr)
{
    ostr << "In the kitchen: " << std::endl;

    ingredients.sort(sortFunction);

    std::list<Ingredient>::iterator itr;
    for (itr = ingredients.begin(); itr != ingredients.end(); ++itr)
    {
        ostr << std::setw(3) << std::right << itr->getQuantity() << " " 
        << itr->getName() << std::endl;

    }
}
bool排序函数(常量成分a、常量成分b)
{
如果(a.getQuantity()ostr可能有另一个
排序功能
(例如在
厨房
)导致上述错误

试一试

类似于

此外,为了获得良好的编码实践,您可能需要进行更改

bool sortFunction(const Ingredient a, const Ingredient b)


第一个是传递对象的副本,第二个只是传递对它的引用。

可能有另一个
sortFunction
在某处(例如在
Kitchen
中),这会导致上述错误

试一试

类似于

此外,为了获得良好的编码实践,您可能需要进行更改

bool sortFunction(const Ingredient a, const Ingredient b)


第一个是传入对象的副本,第二个只是传递对它的引用。

看起来Kicthen中有一个名为sortFunction的方法,编译器无法选择合适的方法。 您可以尝试以下方法:

list.sort( ::sortFunction );
要解决这个问题,或者如果您提供的函数假设是Kitchen类的方法,那么您需要修复它

顺便说一句:

if(a.getName()
同:

return a.getName() < b.getName();
返回a.getName()
看起来Kicthen中有一个名为sortFunction的方法,编译器无法选择合适的方法。 您可以尝试以下方法:

list.sort( ::sortFunction );
要解决这个问题,或者如果您提供的函数假设是Kitchen类的方法,那么您需要修复它

顺便说一句:

if(a.getName()
同:

return a.getName() < b.getName();
返回a.getName()
我猜您在另一个成员函数(例如
printContents
)中声明了一个成员函数
Kitchen::sortFunction
。该函数将隐藏您要使用的非成员函数

错误消息表明情况就是这样;它正在尝试为成员函数类型
bool(Kitchen:*)(const-component&,const-component&)实例化
sort


如果成员函数不应该存在,则删除声明。如果存在,则重命名其中一个函数,或将非成员函数引用为
::sortFunction

我猜您在另一个成员函数中声明了成员函数
Kitchen::sortFunction
(例如
printContents
),它将隐藏要使用的非成员函数

错误消息表明情况就是这样;它正在尝试为成员函数类型
bool(Kitchen:*)(const-component&,const-component&)实例化
sort

如果成员函数不应存在,则删除该声明。如果存在,则重命名其中一个函数,或将非成员函数引用为
::sortFunction
排序函数是:

bool sortFunction(const Ingredient a, const Ingredient b)
但可能是:

bool sortFunction(const Ingredient &a, const Ingredient &b)
(请注意参考资料)

另外,如前所述,您的Kitchen类已经有一个名为sortFunction()的函数,并且它具有优先权,因此请使用::sortFunction()或为每个函数指定一个唯一且更具描述性的名称

如果Kitchen::sortFunction()是您想要的函数,则它必须是静态成员函数。

您的排序函数是:

bool sortFunction(const Ingredient a, const Ingredient b)
但可能是:

bool sortFunction(const Ingredient &a, const Ingredient &b)
(请注意参考资料)

另外,如前所述,您的Kitchen类已经有一个名为sortFunction()的函数,并且它具有优先权,因此请使用::sortFunction()或为每个函数指定一个唯一且更具描述性的名称


如果您想要的是Kitchen::sortFunction(),则它必须是静态成员函数。

如何定义排序?错误是没有匹配的调用。@TopGunCoder:
std::list
有一个
sort()
采用比较器的函数。您是否有其他名为
sortFunction
的函数?
sortFunction
Kitchen
的非静态成员?为什么您既有
bool sortFunction(const component a,const component b)
又有
bool Kitchen::sortFunction(const component&,const component&)
?如何定义排序?错误是没有匹配的调用。@TopGunCoder:
std::list
有一个
sort()
采用比较器的函数。您是否有其他名为
sortFunction
的函数?
sortFunction
Kitchen
的非静态成员?为什么您既有
bool sortFunction(const component a,const component b)
又有
bool Kitchen::sortFunction(const component&,const component&)
?你确定原因吗?我以前有过这样的经历,但它给了我同样的错误。:(你确定原因吗?我以前有过这样的经历,但它给了我同样的错误。:)(