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



我是第一次做oop,我正在为一个名为PersonList的类制作一些成员函数。我只完成了由PersonList类组成的类,并且(到目前为止(我已经完成了该类中的所有其他内容。问题是排序函数不会编译,这是PersonList:的类定义

包括"Person.h">

包括

类PersonList{私有:

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);

};

endif//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 Studio2019CommunityVCToolsMSVC14.23.28105includexutility(617): error C2064: term does not evaluate to a function taking 2 arguments
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includexutility(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 Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(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 Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(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:Userskarljkagy1901_solutions_vt20Laboration_3srcPersonList.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 Studio2019CommunityVCToolsMSVC14.23.28105includexutility(617): error C2056: illegal expression
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(3376): error C2064: term does not evaluate to a function taking 2 arguments
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(3380): error C2064: term does not evaluate to a function taking 2 arguments
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(3390): error C2064: term does not evaluate to a function taking 2 arguments
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includealgorithm(3402): error C2064: term does not evaluate to a function taking 2 arguments
NMAKE : fatal error U1077: 'C:PROGRA~2MICROS~42019COMMUN~1VCToolsMSVC1423~1.281binHostx86x86cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostX86x86nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostX86x86nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105binHostX86x86nmake.exe"' : return code '0x2'

请帮帮我,我真的不明白问题出在哪里,为什么它说它不计算为一个接受2个参数的函数,我以为我已经在排序器中给出了它。

请记住,非静态成员函数需要调用一个对象。调用您的方法如下:
PersonList a,b,c;
a.sNameSorter(b,c);

但是,您希望比较2个元素,而不是3个。简单的解决方法是将这些比较函数声明为static,这样就可以在没有实例的情况下调用它们。

此外,你在sNameSorter中对i的所有表现看起来都有点奇怪。似乎唯一影响返回值的是最后几行:

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

这里的问题是,如果条件是false,则不返回值。我想你想要的是:

return lhsFirst < rhFirst;

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

最新更新