C++ 运算符 == 的模板类错误

发布于 2024-07-16 18:27:23 字数 907 浏览 6 评论 0原文

错误:
错误 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;
}

indexList.h
indexList.cpp

这是假设成为一个重载运算符? 作为模板类,我不确定我是否理解该错误?

解决方案- 类中的重载函数现在声明为 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;
}

indexList.h
indexList.cpp

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回忆追雨的时光 2024-07-23 18:27:23

首先按原样阅读错误文本:

二进制“==”:找不到采用“const Entry”类型的左侧操作数的运算符

这意味着它找不到任何接受 entry== 运算符> 输入作为其左操作数。 此代码无效:

entry const e;
if (e == foo)

您已向我们展示了列表类的代码,但这不是错误的原因。 该错误是关于缺少 entry 类型的运算符(无论是什么)。 要么为类提供一个 operator== 函数,要么声明一个独立的 operator== 函数,该函数接受 const Entry& 作为其第一个参数。

struct entry {
  bool operator==(const entry& other) const;
};
// or
bool operator==(const entry& lhs, const entry& rhs);

我认为后者是首选风格。

Start by reading the error text exactly as it is:

binary '==' : no operator found which takes a left-hand operand of type 'const entry'

It means it can't find any == operator that accepts an entry type as its left operand. This code isn't valid:

entry const e;
if (e == foo)

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 an operator== function, or declare a standalone operator== function that accepts a const entry& as its first parameter.

struct entry {
  bool operator==(const entry& other) const;
};
// or
bool operator==(const entry& lhs, const entry& rhs);

I think the latter is the preferred style.

酷到爆炸 2024-07-23 18:27:23

问题是指在此实例中使用的类型 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.

无声情话 2024-07-23 18:27:23

这很可能是“const 中毒”,即在搜索函数的声明中使用 const 将迫使您添加所有的 const 版本您调用的下游函数。

在声明为 const 的函数中,this 指针被视为 const,这意味着您通过该指针使用的所有成员都被视为 const 也是如此。 如果您专门使用的任何类型 T 的 operator == () 没有显式指定 const 参数,您将收到此错误。

如果您不能确保您使用的所有 T 都具有正确的 operator == () 调用,我会删除成员函数模板上的 const 说明符。

This is likely "const poisoning", where your use of const in the declaration of your search function will force you to add const versions of all the downstream functions you call.

In a function declared const, the this pointer is considered const, which means that all the members you use through that pointer are considered const as well. If your operator == () for whatever type T you are specializing with doesn't explicitly specify const 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.

硬不硬你别怂 2024-07-23 18:27:23

您用作此类参数的类型 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.

音栖息无 2024-07-23 18:27:23

默认情况下,未定义用户定义类型的相等运算符。 这与您的模板类无关,而是与您的 structclass “条目”有关。

因此,您必须重写结构条目类条目中的相等运算符。

或者,如果您不想强制所有使用该模板的内容定义相等运算符,则可以修改模板接口以接受执行相等比较的 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 or class "entry".

Therefore in you'll have to override the equality operator in struct entry or class 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.

各自安好 2024-07-23 18:27:23

有时候写一下就足够了

...
    if (target == elements[i])
...

Sometimes it is quite enough to write

...
    if (target == elements[i])
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文