2013-03-12 79 views
0

我必须为类项目实现通用二进制搜索函数。测试文件和头文件(类定义)已经提供给我,并且不能修改。C++,通过指针传递泛型数组,继承,错误:没有操作符需要右手操作数

我能够使用我测试过的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个错误,例如:[binary'<'no操作数需要类型'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; 
} 

在做两个人的对象简化测试比较,它们的比较就好了。即:

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 
+0

您没有将'Person'对象传递给搜索,您正在传递一个字符串。你的比较函数(如((searchValue <* list [half])')如何工作? – Joe 2013-03-12 01:45:17

+0

这些是我的想法,但另外两个工作!我很困惑。我认为他们现在都需要下面的解决方案,我认为它的工作。 – SomeRandomDeveloper 2013-03-12 02:20:48

+0

你可以显示头文件吗? – 2013-03-12 02:21:56

回答

1

你有没有办法把字符串转换到一个人,所以像这样的行失败:

 if(searchValue < *list[half]){ 

你”如果您暂时将其更改为:

 if (T(searchValue) < *list[half]){ 

T帽子是此代码可以工作的唯一方式,因为只有operator<可以采取*list[half]需要const T &在另一边。

+1

你真棒的家伙,谢谢。不知道我明白为什么其他两个班没有它,但他们现在都工作。愿施瓦茨与你同在。 – SomeRandomDeveloper 2013-03-12 02:19:13

+0

对不起,他们编译得出结论,但实际上并没有做比较的权利,但重新阅读您的文章让我意识到您的意思是为了帮助我进行调试,谢谢,它帮助我查看对象现在用调试器。 – SomeRandomDeveloper 2013-03-12 13:31:03

相关问题