简体   繁体   English

运算符“==”不能应用于“T”和“int”类型的操作数

[英]Operator '==' cannot be applied to operands of type 'T' and 'int'

what I should do here?我应该在这里做什么?

On node.Item == target the following error occurs: Operator '==' cannot be applied to operands of type 'T' and 'T'在 node.Item == 目标上发生以下错误:运算符“==”不能应用于“T”和“T”类型的操作数

so I need it all as generic所以我需要这一切都是通用的

public class Node<T>
    {
       

        public T  Item  { get; set; }
        public Node<T> Left { get; set; }
        public Node<T> Right { get; set; }
        public Node(T item)
        {
                    Item = item;
        }

        public override string ToString()
        {
            return $"{Item,-15}";
        }
    }

public bool Ancestors(Node<T> node, T target)
{
    if (node == null)
    {
        return false;
    }

    if (node.Item == target)
    {
        return true;
    }

    if (Ancestors(node.Left, target)
        || Ancestors(node.Right, target))
    {
        Console.Write(node.Item + " ");
        return true;
    }

    return false;
}

I assume that you intend for target to have the same generic type as T in Node<T> .我假设您打算让target具有与Node<T>中的T相同的泛型类型。 In that case, you should change your method signature to:在这种情况下,您应该将方法签名更改为:

public bool Ancestors<T>(Node<T> node, T target) where T : IEquatable<T>

You would then compare node.Item to target using:然后,您将使用以下命令将node.Itemtarget进行比较:

if (node.Item.Equals(target))

The complete method could look like this:完整的方法可能如下所示:

public bool Ancestors<T>(Node<T> node, T target) where T : IEquatable<T>
{
    if (node?.Item == null)
        return false;

    if (node.Item.Equals(target))
        return true;

    if (Ancestors(node.Left, target) ||
        Ancestors(node.Right, target))
    {
        Console.Write(node.Item + " ");
        return true;
    }

    return false;
}

Let's focus on this line:让我们关注这一行:

if (node.Item == target)

In order to compare two items, they must be of the same type (some exceptions apply to this rule, but that's irrelevant for your situation).为了比较两个项目,它们必须属于同一类型(此规则有一些例外情况,但这与您的情况无关)。

So let's confirm that the two things we're comparing are of the same type.因此,让我们确认我们正在比较的两个事物属于同一类型。

target is easy, because your method parameter clearly states int target . target很简单,因为您的方法参数清楚地说明了int target

So what is node.Item ?那么什么是node.Item Well, first, let's look at what node is in the method declaration:好吧,首先我们来看看方法声明中的node是什么:

Node<T> node

You also posted your Node<T> class definition:您还发布了Node<T> class 定义:

public class Node<T> 
{ 
    public T Item { get; set; } 
}

Therefore, the Item property of your Node<T> node is of type T .因此,您的Node<T> nodeItem属性属于T类型。 But wait, result is an int .但是等等, result是一个int We can't compare that!我们没法比!

Hence the error you're getting:因此你得到的错误:

Operator '==' cannot be applied to operands of type 'T' and 'int'运算符“==”不能应用于“T”和“int”类型的操作数

I hope the error makes more sense now.我希望这个错误现在更有意义。


So how do we solve it?那么我们该如何解决呢? Well, one of the operands must change type to match the other operand.好吧,其中一个操作数必须更改类型以匹配另一个操作数。 So we could make both of them int :所以我们可以把它们都int

public bool Ancestors(Node<int> node, int target)
{
    if (node.Item == target)
    {
        return true;
    }
}

or we can make both operands to be of type T :或者我们可以将两个操作数都设为T类型:

public bool Ancestors(Node<T> node, T target)
{
    if (node.Item.Equals(target))
    {
        return true;
    }
}

Note that I'm using Equals here, instead of == , because == doesn't work for all types (and the compiler needs to account for all possible types T could represent).请注意,我在这里使用Equals而不是== ,因为==不适用于所有类型(并且编译器需要考虑T可以表示的所有可能类型)。

The correct solution depends on precisely what you need here.正确的解决方案取决于您在这里需要什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符“ &amp;&amp;”不能应用于类型为“ int”和“ int”的操作数 - Operator “&&” cannot be applied to operands of type 'int' and 'int' 运算符“ &lt;=”不能应用于“ VictoryCountdown”和“ int”类型的操作数 - Operator `<=' cannot be applied to operands of type `VictoryCountdown' and `int' 运算符&#39;+&#39;不能应用于&#39;byte []&#39;和&#39;int&#39;类型的操作数 - Operator '+' cannot be applied to operands of type 'byte[]' and 'int' 运算符&#39;!=&#39;不能应用于&#39;bool&#39;类型的操作数? 和&#39;int&#39; - Operator '!=' cannot be applied to operands of type 'bool?' and 'int' 运算符&#39;==&#39;不能应用于&#39;int&#39;和&#39;string&#39;类型的操作数 - Operator '==' cannot be applied to operands of type 'int' and 'string' + =运算符不能应用于类型为&#39;int&#39;和&#39;object&#39;的操作数 - += operator cannot be applied to operands of type 'int' and 'object' 接线员&#39;??&#39; 不能应用于&#39;string&#39;和&#39;int&#39;类型的操作数 - Operator '??' cannot be applied to operands of type 'string' and 'int' 运算符 &amp;&amp; 不能应用于“int”和“bool”类型的操作数 - Operator && cannot be applied to operands of type 'int' and 'bool' 运算符不能应用于string和int类型的操作数 - Operator cannot be applied to operands of type string and int 运算符“ ||”不能应用于类型为“ bool”和“ int”的操作数 - Operator “||” cannot be applied to operands of type 'bool' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM