【问题标题】:error C2679: binary '==' : no operator found which takes a right-hand operand of type错误 C2679:二进制“==”:未找到采用右侧操作数类型的运算符
【发布时间】:2013-03-10 22:26:24
【问题描述】:

我无法让我的家庭作业正常工作。我已经重载了我的 '==' 运算符,但我仍然收到此错误。不太确定它为什么被抛出或如何修复它。任何帮助将不胜感激。

这是我的算法:

/* 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;

这是我的person类头文件,显示了重载的运算符:

#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 类头文件中添加了一个新的运算符:bool operator ==(const char*&amp; lhs, const Person&amp; rhs) { return lhs == rhs.getName(); } 我仍然遇到同样的错误。也许我误解了你给我的一些答案。

标签: c++ inheritance operator-overloading


【解决方案1】:

当你打电话时

binarySearch(person, "Mickey Mouse", 0, 7)

binarySearchT中,person是指针数组,Vconst char*。然后在你的身体里做

searchValue == *list[mid]

这是const char*&amp; == *person[x],这就是您收到错误的原因,因为没有operator==(const char*, X),其中X*person[x] 的任何内容。

【讨论】:

  • 其实personT*的数组,而不仅仅是T。但这个答案的本质似乎是正确的。
【解决方案2】:

您的模板类适用于TV 类型。在binarySearch 函数中,您获取T 类型的列表和V 类型的搜索值。然后比较它们:if (searchValue == *list[mid])。这就是错误所在,因为您可能没有为 T 类实现 == 运算符,该运算符接受 V 类型的参数。

问题可以追溯到您的cout,您将Person 作为T 类型传入,const char* 作为V 类型传入。您的Person 类'== 运算符只接受同样类型为Person 的右手操作数。也就是说,在表达式a == b中,b必须是Person类型。

【讨论】:

    【解决方案3】:

    if (searchValue == *list[mid]) 行将类型 const V& 与 T 进行比较。 V 是 C 字符串 (char*),假设 person 是 Person* 的数组,T 是 Person。您提供了一个const Person&amp;, const Person&amp; 比较运算符,但代码需要一个const char*&amp;, const Person 比较运算符。要么提供这样的运算符,要么从 binarySearch(person, "Mickey Mouse", 0, 7) 表达式中的字符串创建一个 Person。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多