Compiler Error CS1729

“type”不包含采用“number”参数的构造函数。

在直接或间接调用类的构造函数时,如果编译器找不到具有相同数量参数的任意构造函数,则会发生此错误。在下面的示例中,test 类没有采用任意参数的构造函数。因此,该类只有不含参数的默认构造函数。由于在生成该错误的第二行上,派生类没有声明其自己的构造函数,因此编译器将提供默认构造函数。该构造函数在基类中调用无参数构造函数。由于基类没有此类构造函数,因此会生成 CS1729。

更正此错误

  1. 调整构造函数调用中的参数数量。

  2. 修改相应的类,提供包含必须调用的参数的构造函数。

  3. 在基类中提供无参数构造函数。

下面的示例生成 CS1729:

// cs1729.cs
class Test
{
    static int Main()
    {
        // Class Test has only a default constructor, which takes no arguments.
        Test test1 = new Test(2); // CS1729
        // The following line resolves the error.
        Test test2 = new Test();

        // Class Parent has only one constructor, which takes two int parameters.
        Parent exampleParent1 = new Parent(10); // CS1729
        // The following line resolves the error.
        Parent exampleParent2 = new Parent(10, 1);

        return 1;
    }
}

public class Parent
{
    // The only constructor for this class has two parameters.
    public Parent(int i, int j) { }
}

// The following declaration causes a compiler error because class Parent
// does not have a constructor that takes no arguments. The declaration of
// class Child2 shows how to resolve this error.
public class Child : Parent { } // CS1729

public class Child2 : Parent
{
    // The constructor for Child2 has only one parameter. To access the 
    // constructor in Parent, and prevent this compiler error, you must provide 
    // a value for the second parameter of Parent. The following example provides 0.
    public Child2(int k)
        : base(k, 0)
    {
        // Add the body of the constructor here.
    }
}

请参阅

继承(C# 编程指南)

构造函数(C# 编程指南)