'<型>' に、引数を '<個数>' 個指定できるコンストラクターがありません。
クラスのコンストラクターを直接または間接的に呼び出したが、コンパイラが同じパラメーター数のコンストラクターを検出できない場合、このエラーが発生します。 次の例では、test クラスに引数を受け取るコンストラクターがありません。 したがって、引数を受け取らないパラメーターなしのコンストラクターがあるのみです。 エラーが発生する 2 行目では、派生クラスで独自のコンストラクターを宣言していないため、コンパイラはパラメーターなしのコンストラクターを備えます。 このコンストラクターは、基本クラスのパラメーターなしのコンストラクターを呼び出します。 基本クラスにはこのようなコンストラクターはないため、CS1729 エラーが発生します。
このエラーを解決するには
呼び出しで、パラメーターの数をコンストラクターに合わせて調整します。
クラスを変更して、パラメーターを備えた、呼び出す対象のコンストラクターを用意します。
基本クラスでパラメーターなしのコンストラクターを用意します。
例
次の例では CS1729 エラーが生成されます。
// cs1729.cs
class Test
{
static int Main()
{
// Class Test has only a parameterless 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.
}
}
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET