构造函数定义:接收"no argument given"

Contructor definition: receiving "no argument given"

我正在尝试创建派生的 class,但我收到每个构造函数的语法错误。

There is no argument given that corresponds to the required formal parameter 'p' of 'Parent.Parent(Parent)'

这对我来说没有任何意义。这是构造函数定义而不是方法调用我以前从未在不是调用的东西上看到过。

namespace ConsoleApp1
{

        public class Parent
        {
            public string Label;

            public Parent(Parent p)
            {
                Label = p.Label;
            }
        }

        public class Child : Parent
        {
            public string Label2;

            public Child(Parent p)
            {
                Label = p.Label;
            }

            public Child(Child c)
            {
                Label = c.Label;
                Label2 = c.Label2;
            }

            public Child(string blah, string blah2)
            {
                Label = blah;
            }
        }
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

这个:

public LabelImage(LabelImage source)
{
    Label = source.Label;
    image = new MagickImage(source.image);
    fileinfo = source.fileinfo;
}

隐式是这样的:

public LabelImage(LabelImage source) : base()
{
    Label = source.Label;
    image = new MagickImage(source.image);
    fileinfo = source.fileinfo;
}

注意 base() 部分,尝试调用 MyImageAndStuff 无参数构造函数,或者只有 params 数组参数的构造函数,或者只有可选参数的构造函数。不存在这样的构造函数,因此出现错误。

你可能想要:

public LabelImage(LabelImage source) : base(source)
{
    Label = source.Label;
    image = new MagickImage(source.image);
    fileinfo = source.fileinfo;
}

...以及所有其他构造函数的类似内容。要么,要么您需要向 MyImageAndStuff 添加一个无参数构造函数。如果 已经有 MyImageAndStuff 的实例,你就不能创建 MyImageAndStuff 的实例,这似乎很奇怪——尽管我猜 source 可以为空。

因为 MyImageAndStuff 没有无参数构造函数或无需传递任何参数即可解析的构造函数,因此您需要在 LabelImage 内的所有派生构造函数中显式调用 MyImageAndStuff 的构造函数。示例:

  public LabelImage(LabelImage source)
    : base(source)