对于' ...'的最佳超载方法有一些无效的论点

时间:2014-11-10 04:47:38

标签: c# operator-overloading

我创建了一个播放器类,我正在尝试使用我的播放器类为我的菜单驱动的播放器程序创建和插入播放器,但我保留错误: 'Assignment_7.Program.GetInsertIndex(int Assignment_7.Player,ref int)'的最佳重载方法与我的InsertPlayer和我的ProcessCreate方法有一些无效的参数

任何帮助都将不胜感激。

 static void ProcessCreate(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
    {

        if (playerCount < MAXPLAYERS)
        {
            Console.WriteLine("\nCreate Player: please enter the player's number");
            number = Int32.Parse(Console.ReadLine());
            if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, playerCount) == -1)
            {
                Console.WriteLine("\nCreate Player: please enter the player's First Name");
                 firstName = Console.ReadLine();
                 Console.WriteLine("\nCreate Player: please enter the player's First Name");
                 lastName = Console.ReadLine();
                Console.WriteLine("\nCreate Player: please enter the player's goals");
                goals = Int32.Parse(Console.ReadLine());
                Console.WriteLine("\nCreate Player: please enter the player's goals");
                assists = Int32.Parse(Console.ReadLine());
                InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
                Console.WriteLine("\nCreate Player: Number - {0}, First Name - {1},LastName - {2} Goals - {3}, Assists {4} created successfully", number,firstName, lastName, goals, assists);
                Console.WriteLine();
            }
            else
                Console.WriteLine("\nCreate Player: the player number already exists");
        }
        else
            Console.WriteLine("\nCreate Player: the player roster is already full");

    }


    //Inserts the player at the correct location in the tables based on order of ascending player number
    //Unless the insert location is at the end, this requires shifting existing players down in order to make room 
    //Inserts the player at the correct location in the tables based on order of ascending player number
    //Unless the insert location is at the end, this requires shifting existing players down in order to make room 
    static Int32 InsertPlayer(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount)
    {
        Int32 insertIndex, shiftCount;
   ---> insertIndex = GetInsertIndex(number, players, playerCount); <--- Error
        for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
            players[shiftCount] = players[shiftCount - 1];
        players[insertIndex] = new Player(firstName, lastName, number, goals, assists);
        playerCount++;
        return insertIndex;
    }





    //Returns the index of the first player number in the table that is greater
    //than the player number to be inserted
    static Int32 GetInsertIndex(Int32 number,Player[] players,
        ref Int32 playerCount)
    {
        Int32 index = 0;
        bool found = false;
        while (index < playerCount && found == false)
            if (players[index].Number > number)
                found = true;
              else
                index++;
        return index;
    }

    //Returns the index of the player number in the table 
    //or -1 if the number is not found
    static Int32 GetPlayerIndex(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount)
    {
        Int32 index = 0;
        bool found = false;
        while (index < playerCount && found == false)
             if (players[index].Number == number)
                found = true;
              else
                index++;
        if (found == false)
            index = -1;
        return index;
    }

3 个答案:

答案 0 :(得分:1)

方法GetInsertIndex期望最后一个参数带有ref修饰符,但是您要按值传递它。您应该在传递参数之前添加ref

insertIndex = GetInsertIndex(number, players, ref playerCount);
//                                      here  ^^^

答案 1 :(得分:1)

根据MSDNref必须在声明和调用中使用

  

要使用ref参数,方法定义和调用方法都必须显式使用ref关键字...

因此,在您的情况下,您应该替换

insertIndex = GetInsertIndex(number, players, playerCount);

insertIndex = GetInsertIndex(number, players, ref playerCount);

答案 2 :(得分:0)

在方法参数中使用ref时,必须在调用中使用它。然后替换

insertIndex = GetInsertIndex(number, players, playerCount);

insertIndex = GetInsertIndex(number, players, ref playerCount);
相关问题