我无法让我的家庭作业正常工作。我已经重载了我的 '==' 运算符,但我仍然收到此错误。不太确定为什么会抛出它或如何修复它。任何帮助,将不胜感激。
这是我的算法:
/* Performs a recursive binary search on the given array. It returns
* the index of the found value, -1 otherwise.
*/
template <typename T, typename V>
int binarySearch(T* list[], const V& searchValue,
const int firstIndex, const int lastIndex)
{
if (firstIndex <= lastIndex)
{
int mid = (firstIndex + lastIndex) / 2; //mid point of list.
if (searchValue == *list[mid])
return mid; // found value.
else if (searchValue < *list[mid])
return binarySearch(list, firstIndex, mid - 1, searchValue);
else
return binarySearch(list, mid + 1, lastIndex, searchValue);
}
return -1; //failed to find value
}
调试器说 main 中的这一行是错误的来源:
// Search the person array.
cout << "Searching people array for person with name = 'Mickey Mouse': "
<< (binarySearch(person, "Mickey Mouse", 0, 7) != -1? "found it." : "did not find it.")
<< endl;
这是我的人员类头文件,显示了重载的运算符:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
using namespace std;
namespace P03 {
class Person {...}; // end Person
/* Displays a Person to the screen.
* Calls the toString() method.
*/
ostream& operator <<(ostream& out, const Person& person)
{
return out << person.toString();
}
/* 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)
{
return lhs.getName() == rhs.getName();
}
/*other operators*/
...
} // end namespace P03
#endif
不确定是否需要更多我的代码。如果需要,我会更新。