我无法让我的作业分配正常工作。我重载了我的'=='运算符,但仍然出现此错误。不知道为什么它被抛出或如何解决它。任何帮助,将不胜感激。错误C2679:二进制'==':没有找到操作符,它需要
这里是我的算法:
/* 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
}
调试器说,此行主要是在错误来源:
// 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
不知道是否需要更多我的代码。如有需要,我会进行更新。
我加了一个新的运营我的Person类的头文件: '布尔运算符==(为const char * LHS,常量人及右) \t { \t \t回LHS == rhs.getName(); \t}' 我仍然收到同样的错误。也许我误解了你给我的一些答案。 – user2069621 2013-03-10 23:36:36