我是第一次做oop,我正在努力为名为PersonList的类制作一些成员函数。我只完成了PersonList类组成的类,到目前为止,该类中的所有其他内容都可以使用。问题是排序函数无法编译,这是PersonList的类定义:

包括“Person.h”

包括

类PersonList {
私有(private)的:

std::vector<Person> dataVector;
std::string fileName;

public:

// Member functions
std::string getFileName () const;
void setFileName (std::string pFileName);
Person getPerson (const PersonList, size_t pIndex)const;
size_t getNumberOfEntries (const PersonList) const;
void sortName ();
void sortPersnr ();
void sortShoenr ();
void readFromFile ();
void writeToFile (const PersonList);

void addPerson(Person);
bool sNameSorter (Person const& lhs, Person const&rhs);
bool sPersnrSorter (Person const& lhs, Person const &rhs);
bool sShoenrSorter (Person const&lhs, Person const&rhs);

};

结束// DT019G_PERSONLIST_H

这是排序功能:
// sorts the database according to the specified sorter.
void PersonList::sortName (){
    std::sort(dataVector.begin(), dataVector.end(), &PersonList::sNameSorter);
}
// sorts the database according to the specified sorter.
void PersonList::sortPersnr (){
    std::sort(dataVector.begin(), dataVector.end(), &PersonList::sPersnrSorter);
}
// sorts the database according to the specified sorter.
void PersonList::sortShoenr (){
    std::sort(dataVector.begin(), dataVector.end(), &PersonList::sShoenrSorter);
}

这是分类器功能:
bool PersonList::sNameSorter (Person const& lhs, Person const&rhs){
    std::string lhLast=lhs.name.getLastName(), rhLast=rhs.name.getLastName(), lhFirst=lhs.name.getFirstName(),
    rhFirst=rhs.name.getFirstName();
    for (char & i : lhLast){
        if (i>64 && i<91)
            i=i+32;}
    for (char & i : rhLast){
        if (i>64 && i<91)
            i=i+32;}
    if (lhLast != rhLast)
        return lhLast < rhLast;
    for (char & i : lhFirst){
        if (i>64 && i<91)
            i=i+32;}
    for (char & i : rhFirst){
        if (i>64 && i<91)
            i=i+32;}
    if (lhFirst != rhFirst)
        return lhFirst < rhFirst;
}
// Use the given values and check which is larger, returns a bool with the answer for the sort function to deal with.
// I have to chose what happens if the values are the same, otherwise it will throw an exception.
// (The sort demands this)
bool PersonList::sPersnrSorter (Person const& lhs, Person const&rhs) {
    if (lhs.getPersNr()==rhs.getPersNr())
        return lhs.getPersNr()>rhs.getPersNr();
    if (lhs.getPersNr()!=rhs.getPersNr())
        return lhs.getPersNr()>rhs.getPersNr();
}
// Use the given values and check which is larger, returns a bool with the answer for the sort function to deal with.
// I have to chose what happens if the values are the same, otherwise it will throw an exception.
// (The sort demands this)
bool PersonList::sShoenrSorter(Person const &lhs, Person const &rhs) {
    if (lhs.getSkoNr()==rhs.getSkoNr())
        return rhs.getSkoNr()>rhs.getSkoNr();
    if (lhs.getSkoNr()!= rhs.getSkoNr())
        return lhs.getSkoNr()>rhs.getSkoNr();
}

错误消息如下:
PersonList.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(617): error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(616): note: see reference to function template instantiation 'bool std::_Debug_lt_pred<_Pr&,Person&,Person&,0>(bool(__thiscall PersonList::* &)(const Person &,const Person &),_Ty1,_Ty2) noexcept(<expr>)' being compiled
        with
        [
            _Pr=bool (__thiscall PersonList::* )(const Person &,const Person &),
            _Ty1=Person &,
            _Ty2=Person &
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3440): note: see reference to function template instantiation 'std::pair<_RanIt,_RanIt> std::_Partition_by_median_guess_unchecked<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiled
        with
        [
            _RanIt=Person *,
            _Pr=bool (__thiscall PersonList::* )(const Person &,const Person &)
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3466): note: see reference to function template instantiation 'void std::_Sort_unchecked<Person*,_Fn>(_RanIt,_RanIt,int,_Pr)' being compiled
        with
        [
            _Fn=bool (__thiscall PersonList::* )(const Person &,const Person &),
            _RanIt=Person *,
            _Pr=bool (__thiscall PersonList::* )(const Person &,const Person &)
        ]
C:\Users\karlj\kagy1901_solutions_vt20\Laboration_3\src\PersonList.cpp(31): note: see reference to function template instantiation 'void std::sort<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,bool(__thiscall PersonList::* )(const Person &,const Person &)>(const _RanIt,const _RanIt,_Pr)' being compiled
        with
        [
            _Ty=Person,
            _RanIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Person>>>,
            _Pr=bool (__thiscall PersonList::* )(const Person &,const Person &)
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(617): error C2056: illegal expression
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3376): error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3380): error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3390): error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3402): error C2064: term does not evaluate to a function taking 2 arguments
NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~4\2019\COMMUN~1\VC\Tools\MSVC\1423~1.281\bin\Hostx86\x86\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\nmake.exe"' : return code '0x2'

请帮帮我,我真的不太明白问题出在哪里,为什么它说它不对带有2个参数的函数求值,我以为我已经在排序器中给出了。

最佳答案

请记住,非静态成员函数需要调用一个对象。调用方法如下:

PersonList a,b,c;
a.sNameSorter(b,c);

但是,您要比较2个元素,而不是3个元素。简单的解决方法是将这些比较函数声明为static,以便无需实例即可调用它们。

而且,您在i中对sNameSorter的所有选择看起来都有些奇怪。似乎唯一影响返回值的是最后几行:
if (lhFirst != rhFirst)
    return lhFirst < rhFirst;

这里的问题是,如果条件是false,则不返回值。我想你想代替:
return lhsFirst < rhFirst;

如果是lhFrist == rhFirst,它将已经返回false

10-07 18:55
查看更多