[英]Sorting problems for a vector of class objects as a member function
我是第一次做 oop,我正在努力為一個名為 PersonList 的類創建一些成員函數。 我只完成了構成 PersonList 類的類,並且我已經完成了類中的所有其他工作(到目前為止)。 問題是排序函數無法編譯,這是 PersonList 的類定義:
類 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);
};
這是排序函數:
// 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
函數,以便可以在沒有實例的情況下調用它們。
此外,您在sNameSorter
對i
所有sNameSorter
看起來都有些奇怪。 似乎唯一影響返回值的是最后幾行:
if (lhFirst != rhFirst)
return lhFirst < rhFirst;
這里的問題是,如果條件為false
,您不會返回值。 我想你想要:
return lhsFirst < rhFirst;
如果lhFrist == rhFirst
這將已經返回false
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.