c++,通过指针传递泛型数组,继承,错误:没有接受右操作数的操作符

C++, pass generic array by pointer, inheritence, error: no operator which takes a right-hand operand

本文关键字:操作数 操作符 继承 指针 c++ 数组 泛型 错误      更新时间:2023-10-16

我必须实现一个类项目的通用二进制搜索函数。测试文件和头文件(类定义)已经提供给我了,不能修改。

我能够让它与我测试过的3种测试对象类型中的2种一起工作,这就是我难住的地方,我不知道如何进一步排除故障。

这是我的算法:

template <typename T, typename V>
int binarySearch(T* list[], const V& searchValue,
    const int firstIndex, const int lastIndex) { 
        int half = (firstIndex + lastIndex) /2;
        // if not completly split down already
        if(firstIndex != lastIndex && half != 0){ 
            if(searchValue < *list[half]){  
                // lower half of array
                binarySearch(list, searchValue, firstIndex, half); 
            }
            else if(searchValue > *list[half]){     
                // upper half of array
                binarySearch(list, searchValue, ++half, lastIndex); 
            }
        }
        else if(searchValue == *list[half]){
            return half; // found it
        }
        return -1; // didnt find it
}

下面是我的3个对象数组测试用例:

// pointers to child class objects
Customer* customer[] = { new Customer(1002, 100000.50, "F4", "L1"),
    new Customer(1004, 45000.90, "F1", "L3"),
    new Customer(1003, 120000, "F3", "L2"),
    new Customer(1001, 340000, "F2", "L4")
};
// pointers to child class objects
Employee* employee[] = { new Employee(102, 65000, "F2", "L1"),
    new Employee(104, 45000, "F4", "L3"),
    new Employee(103, 120000, "F1", "L2"),
    new Employee(101, 35000, "F3", "L4")
};
// pointers to parent class objects
Person* person[] = { customer[0],
    customer[3],
    employee[3],
    employee[0],
    employee[2],
    customer[1],
    employee[1],
    customer[2]
};

我用每个对象调用函数,像这样:

// Search the customer array. -> WORKS
cout << endl
     << "Searching customer array for customer with cId = 1002: "
     << (binarySearch(customer, 1002, 0, 3) != -1? "found it." : "did not find it.")
     << endl;
// Search the employee array. -> WORKS
cout << "Searching employee array for employee with eId = 105: "
     << (binarySearch(employee, 105, 0, 3) != -1? "found it." : "did not find it.")
     << endl;
// Search the person array. -> OPERATOR ERRORS
cout << "Searching people array for person with name = 'Mickey Mouse': "
     << (binarySearch(person, "Mickey Mouse", 0, 7) != -1? "found it." : "did not find it.")
     << endl;

搜索函数在Employee和Customer对象数组上都运行良好。当试图在Person数组上运行搜索时,我得到了使用的每个比较运算符的3个错误,例如:[二进制'<'没有操作数,它需要类型为'Person'的右操作数…)

我对已经提供的函数定义中的所有三个对象以完全相同的方式实现了操作符重载。在person类中,我实现了以下重载操作符:

bool operator ==(const Person& lhs, const Person& rhs){
    if(lhs.getKeyValue() == rhs.getKeyValue())
        return true;
    return false;
}
bool operator <(const Person& lhs, const Person& rhs){
    if(lhs.getKeyValue() < rhs.getKeyValue())
        return true;
    return false;
}
bool operator >(const Person& lhs, const Person& rhs){
    if(lhs.getKeyValue() > rhs.getKeyValue())
        return true;
    return false;
}

当对两个person对象进行简化测试比较时,它们比较得很好。即:

cout << "test person compare: " << ("mickey mouse" < person[1] ? "true" : "false");

我不知道从这里往哪里走,希望你能指点一下。

编辑:添加(完整的人头文件):

#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
using namespace std;
namespace P03 {
    class Person {
    private:
        string firstName;
        string lastName;
    public:
        /* Initializes the object.
         */
        Person(const string& firstName = "na", const string& lastName = "na");
        /* Getter methods retun the field value.
         */
        string getFirstName() const;
        string getLastName() const;
        /* Returns the eid.
         */
        string getKeyValue() const;
        /* Returns the compound value: <lastName><space><firstName>
         */
        string getName() const;
        /* Setter methods, set the object.
         */
        void setFirstName(const string& firstName);
        void setLastName(const string& lastName);

        /* Returns the object formatted as:
        *  Person{ firstName=<firstName>, lastName=<lastName> }
        */
        virtual string toString() const;
    }; // end Person

    /* Displays a Person to the screen.
     * Calls the toString() method.
     */
    ostream& operator <<(ostream& out, const Person& person);
    /* The following relational operators compare two instances of the
     * Person class. The comparison is made on the compound value of:
     * <lastName><space><firstName>
     */
    bool operator ==(const Person& lhs, const Person& rhs);
    bool operator !=(const Person& lhs, const Person& rhs);
    bool operator <(const Person& lhs, const Person& rhs);
    bool operator <=(const Person& lhs, const Person& rhs);
    bool operator >(const Person& lhs, const Person& rhs);
    bool operator >=(const Person& lhs, const Person& rhs);
} // end namespace P03
#endif

没有办法将字符串转换为人,所以像这样的行失败了:

        if(searchValue < *list[half]){  

如果您将其临时更改为:

,您将获得更好的调试
        if (T(searchValue) < *list[half]){  

这是这个代码可以工作的唯一方式,因为唯一可以占用*list[half]operator<需要另一侧的const T &