【问题标题】:Sorting problems for a vector of class objects as a member function作为成员函数的类对象向量的排序问题
【发布时间】:2020-07-11 03:39:38
【问题描述】:

我是第一次做 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 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 个参数的函数,我以为我已经在我的排序器中给出了它。

【问题讨论】:

    标签: c++ sorting oop vector member-functions


    【解决方案1】:

    请记住,非静态成员函数需要调用对象。调用你的方法是这样的:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      相关资源
      最近更新 更多