C++ 类对象向量作为成员函数的排序问题

C++ 类对象向量作为成员函数的排序问题,c++,sorting,oop,vector,member-functions,C++,Sorting,Oop,Vector,Member Functions,我第一次做面向对象编程,我正在努力为一个名为PersonList的类创建一些成员函数。我只完成了PersonList类所包含的类,并且我已经完成了该类中的所有其他工作(到目前为止)。问题是排序函数无法编译,这是PersonList的类定义: 包括“Person.h” 包括 阶级人格主义者{ 私人: std::vector<Person> dataVector; std::string fileName; public: // Member functions std::strin

我第一次做面向对象编程,我正在努力为一个名为PersonList的类创建一些成员函数。我只完成了PersonList类所包含的类,并且我已经完成了该类中的所有其他工作(到目前为止)。问题是排序函数无法编译,这是PersonList的类定义:

包括“Person.h” 包括 阶级人格主义者{ 私人:

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);
这是分拣机的功能:

// 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();
}
bool PersonList::snamisorter(Person const&lhs,Person const&rhs){
std::string lhLast=lhs.name.getLastName(),rhLast=rhs.name.getLastName(),lhFirst=lhs.name.getFirstName(),
rhFirst=rhs.name.getFirstName();
用于(字符和i:lhLast){
如果(i>64&&i64&&i64&&i64&&irhs.getPersNr();
如果(lhs.getPersNr()!=rhs.getPersNr())
返回lhs.getPersNr()>rhs.getPersNr();
}
//使用给定的值并检查哪个值更大,返回一个bool,其中包含排序函数要处理的答案。
//我必须选择如果值相同会发生什么,否则它将抛出异常。
//(这类要求)
bool人员列表::SSHOENRSTOR(人员常量和左侧、人员常量和右侧){
if(lhs.getSkoNr()==rhs.getSkoNr())
返回rhs.getSkoNr()>rhs.getSkoNr();
if(lhs.getSkoNr()!=rhs.getSkoNr())
返回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'
PersonList.cpp
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(617):错误C2064:术语不计算为包含2个参数的函数
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(616):注意:请参阅正在编译的函数模板实例化“bool std:_Debug_lt_pred(bool(u thiscall PersonList::*&)(const Person&),const Person&),_Ty1,_Ty2)noexcept()”的参考
具有
[
_Pr=bool(uu thiscall PersonList::*)(const Person&,const Person&),
_Ty1=人&,
_Ty2=人&
]
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3440):注意:请参阅正在编译的函数模板实例化“std::pair std::_Partition_by_median_guess_unchecked(_RanIt,_RanIt,_Pr)”
具有
[
_RanIt=人*,
_Pr=bool(uu thiscall PersonList::*)(const Person&,const Person&)
]
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3466):注意:请参阅正在编译的函数模板实例化“void std::_Sort_unchecked(_RanIt,_RanIt,int,_Pr)”的参考
具有
[
_Fn=bool(uu thiscall PersonList::*)(const Person&,const Person&),
_RanIt=人*,
_Pr=bool(uu thiscall PersonList::*)(const Person&,const Person&)
]
C:\Users\karlj\kagy1901\u solutions\u vt20\Laboration\u 3\src\PersonList.cpp(31):注意:请参阅正在编译的函数模板实例化“void std::sort(const _RanIt,const _RanIt,_Pr)”的参考
具有
[
_Ty=人,
_RanIt=std::_向量_迭代器,
_Pr=bool(uu thiscall PersonList::*)(const Person&,const Person&)
]
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xutility(617):错误C2056:非法表达式
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3376):错误C2064:术语不计算为包含2个参数的函数
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3380):错误C2064:术语不计算为包含2个参数的函数
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3390):错误C2064:术语不计算为包含2个参数的函数
C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm(3402):错误C2064:术语不计算为包含2个参数的函数
NMAKE:致命错误U1077:'C:\PROGRA~2\MICROS~4\2019\common~1\VC\Tools\MSVC\1423~1.281\bin\Hostx86\x86\cl.exe':返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\NMAKE.exe”:返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\NMAKE.exe”:返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\NMAKE.exe”:返回代码“0x2”

请帮帮我,我真的不明白问题出在哪里,为什么它说它不计算一个带2个参数的函数,我以为我在我的分类器中给出了它。

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

PersonList a,b,c;
a.sNameSorter(b,c);
但是,您需要比较2个元素,而不是3个。简单的解决方法是将这些比较函数声明为
静态
,这样就可以在没有实例的情况下调用它们

另外,您在
snamestorter
中为
i
编写的所有内容看起来都有点奇怪。似乎唯一影响返回值的是最后几行:

if (lhFirst != rhFirst)
    return lhFirst < rhFirst;

如果
lhFrist==rhFirst
这将返回
false

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

PersonList a,b,c;
a.sNameSorter(b,c);
但是,您需要比较2个元素,而不是3个。简单的解决方法是将这些比较函数声明为
静态
,这样就可以在没有实例的情况下调用它们

还有您在
snamestorter
loo中为
i
撰写的所有作品