为什么我得到“不包含带0参数的构造函数”错误? C#

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

    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" ;
    }
}

谢谢!

最佳答案
这是因为CharityCyclists类没有不带参数的构造函数.

如果您没有定义其他构造函数,C#编译器将为您生成默认构造函数.
如果您自己定义构造函数(就像您一样),C#编译器将不会生成默认构造函数.

如果要允许在没有参数的情况下构造CharityCyclists,请将此代码添加到类中:

public CharityCyclists() 
{}

转载注明原文:为什么我得到“不包含带0参数的构造函数”错误? C# - 代码日志