C# 如何解决“方法''的无重载需要0个参数”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19517794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:10:31  来源:igfitidea点击:

How to fix "No overload for method ' ' takes 0 arguments"?

c#console-application

提问by User

How can I fix this error?

我该如何解决这个错误?

"No overload for method 'output' takes 0 arguments".

“方法 'output' 没有重载需要 0 个参数”。

The error is at the very bottom at "fresh.output();".

错误位于“fresh.output();”的最底部。

I don't know what I'm doing wrong. Can someone tell me what I should do to fix the code?

我不知道我做错了什么。有人可以告诉我应该怎么做才能修复代码吗?

Here is my code:

这是我的代码:

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();
        }
    }
}

回答by Bill Gregg

It's telling you that the method "output" needs arguments. Here's the signature for "output":

它告诉您方法“输出”需要参数。这是“输出”的签名:

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

So if you want to call that you need to pass in four doubles.

因此,如果您想调用它,则需要传入四个双打。

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

Or to use hard coded values as an example:

或者以硬编码值为例:

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

回答by codeling

You're calling the outputmethod with 0 (zero) parameters, but you have declared it to receive 4 double values. The compiler doesn't know what it should call, since there is no outputmethod with no parameter.

您正在output使用 0(零)个参数调用该方法,但您已声明它接收 4 个双精度值。编译器不知道它应该调用什么,因为没有output没有参数的方法。

回答by Mikael ?stberg

All your implementations of method outputtakes arguments. Supply the arguments and you should be able to compile.

你所有的方法实现都output需要参数。提供参数,您应该能够编译。

Like this:

像这样:

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

回答by Chris Mantle

There's no method named outputthat takes 0 arguments, there's only one that accepts 4 arguments. You must pass parameters to output():

没有命名的方法output接受 0 个参数,只有一个接受 4 个参数。您必须将参数传递给output()

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

回答by Manoj Naik

fresh.output()expect 2 parameters and you're not providing them

fresh.output()期望 2 个参数,而您没有提供它们