错误 C2679:二进制"==":找不到采用类型右侧操作数的运算符
error C2679: binary '==' : no operator found which takes a right-hand operand of type
我无法正常工作。我已经重载了我的"=="运算符,但仍然收到此错误。不太确定为什么它被抛出或如何修复它。任何帮助将不胜感激。
这是我的算法:
/* 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
不确定是否需要更多我的代码。如果需要,我会更新。
当你打电话时
binarySearch(person, "Mickey Mouse", 0, 7)
在binarySearch
中,T
person
的类型是指针数组,V
是const char*
。然后在身体里你做
searchValue == *list[mid]
这是const char*& == *person[x]
,这就是为什么你得到错误,因为没有operator==(const char*, X)
X
在哪里是什么*person[x]
。
您的模板类适用于类型 T
和 V
。 在binarySearch
函数中,获取类型为 T
的列表和类型为 V
的搜索值。 然后你比较它们:if (searchValue == *list[mid])
. 这就是错误所在,因为您可能尚未为类 T
实现一个 ==
运算符,该运算符接受类型 V
的参数。
这个问题可以追溯到你的cout
,你传入Person
作为类型 T
和const char*
作为类型 V
。 您的 Person
类的 ==
运算符仅接受同样为 Person
类型的右操作数。 换句话说,在表达式 a == b
中,b
必须是Person
类型。
if (searchValue == *list[mid])
行将 const V& 类型与 T 进行比较。V 是一个 C 字符串 (char*
(,假设那个人是一个数组Person*
T 是一个Person
。您提供了const Person&, const Person&
比较运算符,但代码需要const char*&, const Person
比较运算符。要么提供这样的运算符,要么从binarySearch(person, "Mickey Mouse", 0, 7)
表达式中的字符串创建一个 Person。
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 是否允许三元运算符在C++中计算两个操作数?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 如何摆脱逗号运算符的这个错误右操作数没有效果( wunsued 值)?
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- c++运算符重载-我实际返回的操作数类型是什么
- 没有运算符>>与这些操作数匹配 ifstream