BubbleSort错误 [英] BubbleSort errors

查看:66
本文介绍了BubbleSort错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Error   1   The best overloaded method match for 'Project_210.BubbleSorter.Sort(object[], Project_210.CompareOp)' has some invalid arguments    C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\Project-210\Program.cs 67  13  Project-210
Error   2   Argument '2': cannot convert from 'Project_210.Program.CompareOp' to 'Project_210.CompareOp'    C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\Project-210\Program.cs 67  42  Project-210

using System;
using System.Collections.Generic;
using System.Text;

namespace Project_210
{
    delegate bool CompareOp(object lhs, object rhs);

    class BubbleSorter
    {
        static public void Sort(object[] sortArray, CompareOp gtMethod)
        {
            for (int i = 0; i < sortArray.Length; i++)
            {
                for (int j = i + 1; j < sortArray.Length; j++)
                {
                    if (gtMethod(sortArray[j], sortArray[i]))
                    {
                        object temp = sortArray[i];
                        sortArray[i] = sortArray[j];
                        sortArray[j] = temp;
                    }
                }
            }
        }
    }

    class Employee
    {
        private string name;
        private decimal salary;

        public Employee(string name, decimal salary)
        {
                this.name = name;
                this.salary = salary;
         }
         public override string ToString()
         {
                return string.Format(name + " {0:C)", salary);
         }
         public static bool RhsIsGreater(object lhs, object rhs)
         {
                Employee empLhs = (Employee)lhs;
                Employee empRhs = (Employee)rhs;
                return (empRhs.salary > empLhs.salary) ? true : false;
         }
    }


    class Program
    {
        delegate bool CompareOp(object ihs, object rhs);

        static void Main(string[] args)
        {
            Employee[] employees =
                {
                    new Employee("Karli Watson",20000),
                    new Employee("Bill Gates",10000),
                    new Employee("Simon Robinson",25000),
                    new Employee("Mortime",(decimal)100000),
                    new Employee("Arabe Jones",20000),
                    new Employee("Avon from <code>'</code>;Black's 7'",50000)
                };
            CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
            BubbleSorter.Sort(employees, employeeCompareOp);

            for (int i = 0; i < employees.Length; i++)
                Console.WriteLine(employees[i].ToString());
        }
    }
}

推荐答案

您已在名称空间和类Program中声明了CompareOp委托.编译器看到了它们两者,并告诉您即使它们实际上是相同的,他也无法说出您实际上要使用这两个中的哪一个.只需在Program类中删除您的委托类型.


祝你好运!
You have declared the CompareOp delegate in your namespace and in your class Program. The compiler sees them both and tells you he is unable to tell which of these two you actually mean to use even though they are practically the same. Simply remove your delegate type in the Program class.


Good luck!


这篇关于BubbleSort错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆