为什么我会得到一个“a”;不包含接受0个参数的构造函数";错误?C#

为什么我会得到一个“a”;不包含接受0个参数的构造函数";错误?C#,c#,constructor,arguments,C#,Constructor,Arguments,在我的表单加载中,我有以下代码: 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);

在我的表单加载中,我有以下代码:

    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;
    }
但是,我收到一个错误,它说“'CycleeEvent.CharityCyclists'不包含接受0个参数的构造函数”,它说错误与这部分代码有关:

CharityCyclists cyclist1 = new 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()
{
    this.bicycle = "bike";
    this.wheels = 2;
    this.FundsRaised = 0;
}

或者类似的东西,这是因为CharityCyclists类没有不带参数的构造函数

如果不定义其他构造函数,C#编译器将为您生成默认构造函数。 如果您自己定义了一个构造函数(正如您所做的那样),C#编译器将不会生成默认构造函数

如果要允许无参数构造
CharityCyclists
,请将此代码添加到类中:

public CharityCyclists() 
{}

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

如果您没有任何构造函数,C#将为您隐式创建一个空构造函数。这和你写的东西在功能上是一样的:

public CharityCyclists()
{
}

但是,只有在没有构造函数的情况下,它才会这样做。你有一个,所以这不会发生。您需要显式创建不带参数的构造函数。

将其添加到
CharityCyclists
类:

public CharityCyclists()
{
}
无法对行进行编码:

CharityCyclists cyclist1 = new CharityCyclists();

除非您在.NET中有该构造函数。

,否则,如果没有声明其他构造函数,则无参数构造函数将隐式存在。一旦您声明了一个带参数的构造函数,您还必须显式声明另一个不带参数的构造函数,以便代码继续编译。

您正在尝试实例化cyclist1 CharityCyclists类的一个实例,而不带任何参数 参数-需要不带参数的构造函数

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

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

当您为类提供了接受参数的构造函数时,编译器不再创建空构造函数

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

对于没有参数的类,您没有构造函数。您拥有的唯一构造函数接受参数。将此添加到您的类:

public CharityCyclists() { } 

错误的哪个部分你不理解?因为你没有一个构造函数,比如<代码>公共CysCycliStSe(){} /Cord>。我假定海报来自C++背景,编译器会生成默认的cTor。下面的答案将解释C++不生成默认构造函数。@ DealAlFrTy:C++不会在相同的情况下创建默认的Cor。在这个C++和C语言中(和java)的行为是相同的。一旦显式创建构造函数,它将不再生成“默认”构造函数。这是不正确的。基类“Cyclist”是需要参数的类。您需要添加:base(param)语法,以使CharityCyclists没有参数。@GalacticJello:不确定我们是否知道。。。他没有为Cyclist发布代码,它很可能有一个无参数构造函数。如果Cyclists具有无参数构造函数,则driis的代码将正常工作,如果没有,则yes driis的参数化构造函数将需要使用预期参数显式调用基类构造函数。请查看CharityCyclists构造函数,它在末尾隐藏:
public CharityCyclists(字符串类型,整数编号,字符串完成,整数小时,整数分钟,整数秒,字符串自行车,整数车轮,双基金奖励):基本(类型,数字,完成,小时,分钟,秒,基金奖励)
@GalacticJello:这是唯一一个可能的
骑自行车者的构造器
,没有看到
骑自行车者
的完整定义,我们无法知道是否存在其他重载构造器……他可能有
骑自行车者(){}
和骑自行车者(String,int,String,int,int,int,double){}`例如……因此,如果在
Cyclists
上有一个无参数构造函数,那么对
base
的显式调用是不必要的。@James Michael Hare:当然可以,但10次中有9次,这个错误意味着你需要为基类提供参数(以我的经验)。注意“创建默认构造函数”默认的构造函数是编译器在没有其他构造函数的情况下为你生成的构造函数。当你创建一个没有参数的构造函数时,你就创建了一个无参数的构造函数。无量纲构造函数是我创建的编译器或编译器所做的默认值。感谢的是其中一个术语,它可能对不同的人意味着不同的东西,所以最好在谈到C时,用MSDN的描述。“代码>:< /Cord>操作符是另一个。C++开发人员经常称之为“三元”。运算符,但C#显式将其称为“条件”运算符。请注意短语“空构造函数”,通常编译器会生成一个调用基类的无参数构造函数的默认构造函数。它不是真正的空,因为IL可能包括编译器生成的字段初始化等。@JamesMichaelHare感谢您的解释。我不确定IL是什么。您能帮我