【问题标题】:Why am I getting a "does not contain a constructor that takes 0 arguments" error? C#为什么我收到“不包含采用 0 个参数的构造函数”错误? C#
【发布时间】:2012-10-26 22:28:35
【问题描述】:

在我的表单加载时,我有这个代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        CharityCyclists cyclist1 = new CharityCyclists();
        CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500);

        cyclist1.Type = "Novelty Charity Cyclist";
        cyclist1.Number = 1;
        cyclist1.Finished = "Not Finished";
        cyclist1.Hours = 0;
        cyclist1.Mins = 0;
        cyclist1.Secs = 0;
        cyclist1.Bicycle = "Tricycle";
        cyclist1.Wheels = 3;
        cyclist1.FundsRaised = 300;
    }

但是,我收到一条错误消息,提示“'CycleEvent.CharityCyclists' 不包含采用 0 个参数的构造函数”,它表示该错误与这部分代码有关:

CharityCyclists cyclist1 = new CharityCyclists();

这是我的 CharityCyclists 课程:

class CharityCyclists : Cyclists
{
    private string bicycle;
    private int wheels;
    private double fundsRaised;

    public string Bicycle
    {
        get { return bicycle; }
        set { bicycle = value; }
    }

    public int Wheels
    {
        get { return wheels; }
        set { wheels = value; }
    }

    public double FundsRaised
    {
        get { return fundsRaised; }
        set { fundsRaised = value; }
    }

    public CharityCyclists(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, fundsRaised)
    {
        this.bicycle = bicycle;
        this.wheels = wheels;
        this.FundsRaised = fundsRaised;
    }

    public override string ToString()
    {
        return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels" ;
    }
}

谢谢!

【问题讨论】:

  • 错误的哪一部分你不明白?
  • 因为你没有像public CharityCyclists(){}这样的构造函数
  • 我认为海报来自 C++ 背景,编译器将在其中生成默认 c'tor。下面的答案将解释 C# 不会生成默认构造函数。
  • @DonalLafferty:在相同情况下,C++ 也不会创建默认 ctor。在此 C++ 和 C#(和 Java)表现相同。一旦显式创建了构造函数,它将不再生成“默认”构造函数。

标签: c# constructor arguments


【解决方案1】:

这是因为CharityCyclists 类没有不带参数的构造函数。

C# 编译器将为您生成默认构造函数,如果您没有定义其他构造函数。 如果您自己定义构造函数(如您所愿),C# 编译器将不会生成默认构造函数。

如果你想允许CharityCyclists不带参数构造,在类中添加这段代码:

public CharityCyclists() 
{}

【讨论】:

  • 这是不正确的。基类“Cyclist”是期望参数的类。您需要添加 :base(param) 语法以使 CharityCyclists 没有参数。
  • @GalacticJello:不确定我们是否知道......他没有为 Cyclist 发布代码,它很可能有一个无参数的构造函数。如果 Cyclists 有一个无参数构造函数,则 driis 的代码可以正常工作,如果没有,那么是的,driis 的参数化构造函数需要使用预期的参数对基类构造函数进行显式调用。
  • 看一下 CharityCyclists 构造函数,它隐藏在最后:public CharityCyclists(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, fundsRaised)
  • @GalacticJello:这只是Cyclists一个 可能的构造函数,没有看到Cyclists 的完整定义,我们无法知道是否存在其他重载构造函数。 . 例如,他可能有Cyclists() { } 和 Cyclists(String, int, String, int, int, int, double) { }`... 因此,如果在@987654328 上有 无参数构造函数@,对base 的显式调用是不必要的。
  • @James Michael Hare:当然,但是 10 次中有 9 次出现此错误意味着您需要为基类提供参数(根据我的经验)。
【解决方案2】:

当你为你的类提供了一个带参数的构造函数时,编译器不再创建一个空的构造函数。

因此,您不能调用空构造函数,因为它不存在。 您需要在类的代码中显式编写接受 0 个参数的构造函数。

【讨论】:

  • 注意“空构造函数”这个短语,通常编译器会生成一个调用基类的无参数构造函数的默认构造函数。它并不是真正的 empty,因为 IL 可能包含由编译器生成的字段初始化等。
  • @JamesMichaelHare 感谢您的解释。我不确定 IL 是什么。你能帮帮我吗?
  • IL 是所有 .NET 语言编译成的中间语言。
【解决方案3】:

你没有一个不带参数的构造函数。

你需要添加

public CharityCyclists()
{
    this.bicycle = "bike";
    this.wheels = 2;
    this.FundsRaised = 0;
}

或者类似的东西

【讨论】:

    【解决方案4】:

    当您创建不包含 0 个参数的构造函数时,您会自动删除默认的 constructor。您应该创建一个新的默认构造函数(不带参数),这将解决这个问题。

    【讨论】:

      【解决方案5】:

      如果您没有任何构造函数,C# 将为您隐式创建一个空的。它在功能上与您编写的内容相同:

      public CharityCyclists()
      {
      }
      

      只有当你有 no 构造函数时,它才会这样做。你有一个,所以这不会发生。您需要显式创建一个不带参数的构造函数。

      【讨论】:

        【解决方案6】:

        你没有那个没有参数的类的构造函数。您拥有的唯一构造函数带有参数。将此添加到您的课程中:

        public CharityCyclists() { } 
        

        【讨论】:

          【解决方案7】:

          如果你声明了构造函数,类不会自动获得默认构造函数。您需要创建一个无参数构造函数来解决问题,或者调用接受参数的构造函数。

          【讨论】:

          • 注意“创建默认构造函数”,这有点误导。默认构造函数是编译器在不存在其他构造函数时为您生成的构造函数。当您创建一个没有参数的构造函数时,您正在创建一个 无参数 构造函数。 msdn.microsoft.com/en-us/library/aa645608(v=vs.71).aspx
          • @JamesMichaelHare 我想我没有意识到两者有不同的术语。我总是将无参数构造函数称为默认构造函数,无论是我创建它还是编译器创建它。谢谢
          • 它是其中一个术语,对不同的人可能意味着不同的东西,所以在谈到 C# 时最好使用 MSDN 的描述。 ? : 运算符是另一个。 C++ 开发人员经常将其称为“三元”运算符,但 C# 明确地将其称为“条件”运算符。
          【解决方案8】:

          将此添加到CharityCyclists 类:

          public CharityCyclists()
          {
          }
          

          你不能编码这条线:

          CharityCyclists cyclist1 = new CharityCyclists();
          

          除非你有那个构造函数。

          【讨论】:

            【解决方案9】:

            在 .NET 中,如果没有声明其他构造函数,则隐式存在无参数(无参数)构造函数。声明带参数的构造函数后,还必须显式声明另一个不带参数的构造函数,代码才能继续编译。

            【讨论】:

              【解决方案10】:

              您正在尝试将 cyclist1 实例化为 CharityCyclists 类的实例,而没有任何 arguments - 这需要一个没有参数的构造函数。

              但是 CharityCyclists 类的定义只有一个带有 9 个参数的构造函数。由于该构造函数需要 9 个参数,因此 cyclist1 将不匹配该构造函数。您需要一个不带参数的构造函数,如下所示:

              public CharityCyclists()
              {
                  this.bicycle ="";
                  this.wheels = 0;       
                  this.FundsRaised = 0.0;
              }
              

              【讨论】:

                猜你喜欢
                • 2011-11-06
                • 1970-01-01
                • 1970-01-01
                • 2013-02-16
                • 2013-02-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多