C# "error CS1729: 'XXClass' does not contain a constructor that takes 0 arguments"的解决方案

简介:

 

出现这种错误的原因时,没有在子类的构造函数中指出仅有带参构造函数的父类的构造参数。

具体来讲就是:

当子类要重用父类的构造函数时, C# 语法通常会在子类构造函数后面调用 : base( para_type, parameter).

假设父类有一个参数个数为1的构造函数, 没有 0 参构造函数。 子类想要重用这个构造函数, 如果没有写 :base(para_type, parameter), 就会有这个错误。 

因为如果没写, VS 会认为子类是继承父类的 0 参构造函数, 但是由于父类并没有定义 0 参构造函数, 所以就会报错。

另外, 可以在base()中调用一个静态方法来修改子类构造函数的参数在传递给父类构造函数。 如:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  ParentClass 
      public  ParentClass( string  Name) 
      {} 
class  ChildClass 
       public  ChildClass( string  firstName,  string  familyName): base ( CombineName(firstName, familyName))
       {
  } 
       static  string  ConbineName( string  firstName,  string  familyName) 
       {
     return  string .Format( "{0},{1}" , firstName, familyName);
  }
}

 

 

 

参考文章

1.changtianshuiyueC# does not contain a constructor that takes no parameter , 2014-9。

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。




    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5807553.html ,如需转载请自行联系原作者

相关文章
|
4月前
|
存储 JSON 开发框架
给开源大模型带来Function Calling、 Respond With Class
OpenAI 在他的多个版本的模型里提供了一个非常有用的功能叫 Function Calling,就是你传递一些方法的信息给到大模型,大模型根据用户的提问选择合适的方法,然后输出给你,你再来执行。
error : Class declarations lacks Q_OBJECT macro
error : Class declarations lacks Q_OBJECT macro
error: implicit declaration of function ‘read‘ [-Werror,-Wimplicit-function-declaration]
error: implicit declaration of function ‘read‘ [-Werror,-Wimplicit-function-declaration]
176 0
|
Java C++
程序运行问题排查和解决:an instance of ‘std::logic_error‘what(): basic_string::_M_construct null not valid
程序运行问题排查和解决:an instance of ‘std::logic_error‘what(): basic_string::_M_construct null not valid
971 0
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
my Note debug - add to function
Created by Wang, Jerry, last modified on Feb 03, 2015
103 0
my Note debug - add to function