簡體   English   中英

如何修復“方法沒有重載”需要0參數“?

[英]How to fix “No overload for method ' ' takes 0 arguments”?

我該如何解決這個錯誤?

“方法'輸出'沒有重載需要0個參數”。

錯誤位於“fresh.output();”的最底部。

我不知道我做錯了什么。 誰能告訴我應該怎么做才能修復代碼?

這是我的代碼:

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

namespace ConsoleApplication_program
{
    public class Numbers
    {
        public double one, two, three, four;
        public virtual void output(double o, double tw, double th, double f)
        {
            one = o;
            two = tw;
            three = th;
            four = f;
        }
    }
    public class IntegerOne : Numbers
    {
        public override void output(double o, double tw, double th, double f)
        {
            Console.WriteLine("First number is {0}, second number is {1}, and third number is {2}", one, two, three);
        }
    }
    public class IntegerTwo : Numbers
    {
        public override void output(double o, double tw, double th, double f)
        {
            Console.WriteLine("Fourth number is {0}", four);
        }
    }
    class program
    {
        static void Main(string[] args)
        {
            Numbers[] chosen = new Numbers[2];

            chosen[0] = new IntegerOne();
            chosen[1] = new IntegerTwo();

            foreach (Numbers fresh in chosen)
            {
                fresh.output();
            }     
            Console.ReadLine();
        }
    }
}

它告訴你方法“輸出”需要參數。 這是“輸出”的簽名:

public override void output(double o, double tw, double th, double f)

因此,如果你想打電話,你需要傳遞四個雙打。

fresh.output(thing1,thing2,thing3,thing4);

或者使用硬編碼值作為示例:

fresh.output(1,2,3,4);

沒有名為output的方法接受0個參數,只有一個接受4個參數。 您必須將參數傳遞給output()

foreach (Numbers fresh in chosen)
{
    fresh.output(o, tw, th, f);
}

您使用0(零)參數調用output方法,但您已聲明它接收4個雙精度值。 編譯器不知道它應該調用什么,因為沒有沒有參數的output方法。

所有方法output的實現都需要參數。 提供參數,你應該能夠編譯。

像這樣:

fresh.output(1, 2, 3, 4);

fresh.output() 2個參數而你沒有提供它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM