【问题标题】:At least one object must implement IComparable至少一个对象必须实现 IComparable
【发布时间】:2021-05-07 01:35:22
【问题描述】:
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<Player> PlayerList = new SortedSet<Player>();

            while (true)
            {
                string Input;
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1. Create new player and score.");
                Console.WriteLine("2. Display Highscores.");
                Console.WriteLine("3. Write out to XML file.");
                Console.Write("Input Number: ");
                Input = Console.ReadLine();
                if (Input == "1")
                {
                    Player player = new Player();
                    string PlayerName;
                    string Score;

                    Console.WriteLine();
                    Console.WriteLine("-=CREATE NEW PLAYER=-");
                    Console.Write("Player name: ");
                    PlayerName = Console.ReadLine();
                    Console.Write("Player score: ");
                    Score = Console.ReadLine();

                    player.Name = PlayerName;
                    player.Score = Convert.ToInt32(Score);

                    //====================================
                    //ERROR OCCURS HERE
                    //====================================
                    PlayerList.Add(player);


                    Console.WriteLine("Player \"" + player.Name + "\" with the score of \"" + player.Score + "\" has been created successfully!" );
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("INVALID INPUT");
                }
            }
        }
    }
}

所以我不断收到“

至少一个对象必须实现 IComparable。

" 尝试添加第二个播放器时,第一个有效,但第二个无效。 我还必须使用SortedSet,因为这是工作的要求,是学校的工作。

【问题讨论】:

  • 错误告诉你你需要知道什么——你Player类必须实现IComparable接口
  • 这种错误不应该存在,我们有一个很好的类型安全语言是有原因的,SortedSet&lt;T&gt; 应该真的有一个约束 T : IComparable&lt;T&gt;
  • 使用ThenBy() 而不是实现IComparable&lt;&gt;IComparer&lt;&gt;
  • SortedSet 应该确实有一个 T 约束:IComparable - 不,因为那样你就不能在一组对象上使用外部比较器没有自己的。
  • 使用 ThenBy() 而不是实现 IComparable 或 IComparer -- 首先,您的意思是 OrderBy。其次,这无济于事......您仍然需要一个比较器(显然......系统不知道如何在没有被告知的情况下订购 Players)。

标签: c#


【解决方案1】:

好吧,您正在尝试使用SortedSet&lt;&gt;... 这意味着您关心排序。但是听上去你的Player 类型并没有实现IComparable&lt;Player&gt;。那么您希望看到什么样的排序顺序?

基本上,您需要告诉Player 代码如何比较一个玩家和另一个玩家。或者,您可以在其他地方实现IComparer&lt;Player&gt;,并将该比较传递给SortedSet&lt;&gt; 的构造函数,以指示您希望玩家的顺序。例如,您可以:

public class PlayerNameComparer : IComparer<Player>
{
    public int Compare(Player x, Player y)
    {
        // TODO: Handle x or y being null, or them not having names
        return x.Name.CompareTo(y.Name);
    }
}

然后:

// Note name change to follow conventions, and also to remove the
// implication that it's a list when it's actually a set...
SortedSet<Player> players = new SortedSet<Player>(new PlayerNameComparer());

【讨论】:

  • +1,因为这是真正解释 OP 为什么他需要实现 IComparable 的唯一答案
  • 是的,谢谢,我正在尝试这个,但是,我试图比较每个玩家的分数,试图安排它,以便最高分在前等等。但我得到这个错误:错误1不一致的可访问性:参数类型'ConsoleApplication1.Player'比方法'ConsoleApplication1.Comp.Compare(ConsoleApplication1.Player,ConsoleApplication1.Player)更难访问
  • @user1930824:是的,所以您需要将其设为内部类而不是公共类。然后比较分数而不是名字。 (我不会用勺子喂你确切你需要的代码......)
  • @user1930824:那么听起来您没有从您的Compare(或CompareTo)方法返回int 值。在每一步,错误消息都准确地说明了问题所在。学习仔细阅读错误信息是软件开发中一项非常重要的技能。
  • @d453:执行的每个成对比较中至少有一个对象。实际上,只有 一些 元素具有可比性是很奇怪的。
【解决方案2】:

我想这是对这个错误的更一般的答案。

此行将因您收到的错误而失败:

Items.OrderByDescending(t => t.PointXYZ);

但是你可以指定如何直接比较:

Items.OrderByDescending(t => t.PointXYZ.DistanceTo(SomeOtherPoint))

那么你就不需要 IComparable 接口了。 取决于您使用的 API。 就我而言,我有一个 Point 和 DistanceTo 方法。 (Revit API) 但是整数应该更容易确定“大小/位置”。

【讨论】:

  • 指出我试图与错误的值进行比较。
【解决方案3】:

你的 Player 类需要实现 IComparable 接口..

http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx

【讨论】:

    【解决方案4】:

    您的 Player 类必须实现 IComparable 接口。 SortedSet 以排序顺序保存项目,但是如果您没有告诉它如何对它们进行排序(使用 IComparable),它怎么知道排序顺序是什么?

    【讨论】:

      【解决方案5】:

      有时会在您忘记编写订单属性时引发

      SortedSet<Player> players = players.OrderBy(c => c);
      

      但它必须是这样的:

      SortedSet<Player> players = players.OrderBy(c => c.PlayerName);
      

      【讨论】:

        【解决方案6】:

        让您的Player 类实现IComparable

        【讨论】:

        • 展示你的作品让人们彻底了解。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 2015-08-10
        • 2015-06-10
        • 1970-01-01
        相关资源
        最近更新 更多