【问题标题】:How to fix "No overload for method ' ' takes 0 arguments"?如何修复“方法''没有重载需要0个参数”?
【发布时间】:2013-10-31 07:39:17
【问题描述】:

如何解决此错误?

“方法'输出'没有重载需要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();
        }
    }
}

【问题讨论】:

  • fresh.output();这里传参数,应该是 output(double o, double tw, double th, double f);
  • 错误信息不清楚?
  • 在此处发布问题之前,请尝试在您身边解决。
  • @User,你知道overloading是什么吗?

标签: c# console-application


【解决方案1】:

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

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

所以如果你想调用它,你需要传入四个双打。

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

或者以硬编码值为例:

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

【讨论】:

  • 我把它改成了:“fresh.output(double o, double tw, double th, double f);”但我得到了更多的错误。你也可以帮忙吗?
  • 调用方法时,会传入实例。您不必再次声明类型。这是在您声明方法时完成的。你需要了解调用方法和声明方法的区别。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    像这样:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        fresh.output() 需要 2 个参数,但您没有提供它们

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-26
          相关资源
          最近更新 更多