C++ 运算符 == 的模板类错误
错误:
错误 C2678:二进制 '==' :找不到采用 'const Entry' 类型的左操作数的运算符(或者没有可接受的转换)
函数:
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& target) const
{
for (int i = 0; i < maxSize; i++)
if (elements[i] == target) //ERROR???
return i; // target found at position i
// target not found
return -1;
}
这是假设成为一个重载运算符? 作为模板类,我不确定我是否理解该错误?
解决方案- 类中的重载函数现在声明为 const:
//Operators
bool entry::operator == (const entry& dE) const <--
{
return (name ==dE.name);
}
Error:
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const entry' (or there is no acceptable conversion)
The function:
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& target) const
{
for (int i = 0; i < maxSize; i++)
if (elements[i] == target) //ERROR???
return i; // target found at position i
// target not found
return -1;
}
Is this suppose to be an overloaded operator? Being a template class I am not sure I understand the error?
Solution-
The overload function in the class now declared const:
//Operators
bool entry::operator == (const entry& dE) const <--
{
return (name ==dE.name);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先按原样阅读错误文本:
这意味着它找不到任何接受
entry
的==
运算符> 输入作为其左操作数。 此代码无效:您已向我们展示了列表类的代码,但这不是错误的原因。 该错误是关于缺少
entry
类型的运算符(无论是什么)。 要么为类提供一个operator==
函数,要么声明一个独立的operator==
函数,该函数接受const Entry&
作为其第一个参数。我认为后者是首选风格。
Start by reading the error text exactly as it is:
It means it can't find any
==
operator that accepts anentry
type as its left operand. This code isn't valid:You've showed us the code for your list class, but that's not what the error is about. The error is about a lack of operators for the
entry
type, whatever that is. Either give the class anoperator==
function, or declare a standaloneoperator==
function that accepts aconst entry&
as its first parameter.I think the latter is the preferred style.
问题是指在此实例中使用的类型 T 未定义运算符==。 我从你的问题中猜测这是一个类“条目”。
也可能是“entry”类没有正确定义operator==来获取constentry& 作为参数。
The problem refers to the type T that is being used in this instance not having an operator== defined. I would guess from your question that this is a class 'entry'.
It might also be that the 'entry' class does not have the operator== defined correctly to take a const entry& as a parameter.
这很可能是“
const
中毒”,即在搜索函数的声明中使用const
将迫使您添加所有的const
版本您调用的下游函数。在声明为
const
的函数中,this
指针被视为const
,这意味着您通过该指针使用的所有成员都被视为const 也是如此。 如果您专门使用的任何类型 T 的
operator == ()
没有显式指定const
参数,您将收到此错误。如果您不能确保您使用的所有 T 都具有正确的
operator == ()
调用,我会删除成员函数模板上的 const 说明符。This is likely "
const
poisoning", where your use ofconst
in the declaration of your search function will force you to addconst
versions of all the downstream functions you call.In a function declared
const
, thethis
pointer is consideredconst
, which means that all the members you use through that pointer are consideredconst
as well. If youroperator == ()
for whatever type T you are specializing with doesn't explicitly specifyconst
parameters, you will get this error.If you can't ensure that all Ts you use will have the proper
operator == ()
calls, I'd remove the const specifiers on the member function templates.您用作此类参数的类型 T 应该具有
operator==()
,因为您提供的代码不包含模板的实例化,因此很难知道出了什么问题。另一方面,模板的函数定义应与类一起位于 .h 文件中,否则编译器将无法正确实例化它。
The type T you're using as a parameter to this class should have an
operator==()
since the code you give doesn't contain the instantiation of the template its difficult to know what's wrong.On a different note, the function definitions of a template should be in the .h file along with the class or else the compiler would not be able to instantiate it properly.
默认情况下,未定义用户定义类型的相等运算符。 这与您的模板类无关,而是与您的
struct
或class
“条目”有关。因此,您必须重写
结构条目
或类条目
中的相等运算符。或者,如果您不想强制所有使用该模板的内容定义相等运算符,则可以修改模板接口以接受执行相等比较的 Comparator。
The equality operator for user-defined types is not defined by default. This doesn't have anything to do with your template class, but rather your
struct
orclass
"entry".Therefore in you'll have to override the equality operator in
struct entry
orclass entry
.Alternatively, if you don't want to force everything that uses that template to define an equality operator, you can modify the template interface to accept a Comparator that does the equality comparison.
有时候写一下就足够了
Sometimes it is quite enough to write