C# 承包商定义:接收“未给出任何参数”
我正在尝试创建一个派生类,并且收到每个构造函数的语法错误 没有给出与所需的形式化参数相对应的参数 “Parent.ParentParent”的参数“p” 这对我来说毫无意义。这是一个构造函数定义,不是一个方法调用,我以前从未在非调用的东西上看到过C# 承包商定义:接收“未给出任何参数”,c#,visual-studio-2017,C#,Visual Studio 2017,我正在尝试创建一个派生类,并且收到每个构造函数的语法错误 没有给出与所需的形式化参数相对应的参数 “Parent.ParentParent”的参数“p” 这对我来说毫无意义。这是一个构造函数定义,不是一个方法调用,我以前从未在非调用的东西上看到过 namespace ConsoleApp1 { public class Parent { public string Label; public Parent(Par
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) : base()
{
Label = source.Label;
image = new MagickImage(source.image);
fileinfo = source.fileinfo;
}
注意基本部分,尝试调用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)
将来,最好将您的问题简化为[mcve]——在本例中,是一个具有参数化构造函数的基类,理想情况下是一个公共类型(如字符串)和一个具有单个构造函数的派生类。将错误消息显示为文本而不是图像…我确实将错误消息显示为文本…因此您不需要将其显示为图像。该图像实际上没有添加任何内容,只是显示它的类名有红色的曲线-你可以很容易地描述这一点。我喜欢视觉效果,据我所知,只要你还以文本形式提供错误消息,就没有任何规则禁止它。这不是规则-但我认为它不会添加任何内容,然而,将其减少为a肯定会改善它。@Nabren是正确的,因为基类没有定义默认构造函数。你给出了一个更详细的答案,所以你得到了答案。请注意,在描述编译器为你提供的构造函数时,C规范中通常使用术语“默认构造函数”。在本例中,您的意思是无参数构造函数-但是只有一个params数组参数或只有可选参数的构造函数也可以。尽管如此,这仍然不正确-如果只有MyImageAndStuff MyImageAndStuff source=null,它就不会有无参数构造函数,但它会编译。另一方面,修正它会使你的答案基本上成为我的一个子集。。。
public LabelImage(LabelImage source)
: base(source)