2013-03-12 32 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

相關問題