对象不包含带有0个参数的构造函数 [英] object does not contain a constructor that takes 0 arguments

查看:107
本文介绍了对象不包含带有0个参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
我有一个继承自另一个类的类,但出现此令人困惑的错误:

HI
I have a class that inherits from another one but i get this confusing error :

Error   1  'WindowDecorator.Decorator' does not contain a constructor that takes 0 arguments



构造函数中发生错误.



Error occurs in the constructor.

public class ScrollDecorator:Decorator
    {
           protected Decorator decorator;

           protected float ScrollPosition;


       protected ScrollDecorator(Decorator _decorator)
        {
            this.decorator = _decorator;
        }


       public  void ScrollTo()
       {
           //return "";
       }

       public override void Draw()
       {

       }
    }

推荐答案

原因是基类Decorator 不包含parameter less构造函数,即采用0个参数的构造函数.

调用此derived class的构造函数时,它将调用基类的参数较少的构造函数,因为没有对基类的构造函数的显式调用,并且由于基类中没有参数较少的构造函数,因此会发生上述错误.

为避免此错误,请使用基类中可用的构造函数所需的适当参数调用基类的构造函数,如下所示:
The reason is that the base class Decorator does not contain a parameter less constructor i.e. a constructor which takes 0 arguments.

When the constructor of this derived class is called it will call the parameter less constructor of the base class as there is no explicit call to the constructor of the base class and since there is no parameter less constructor in the base class the above error occurs.

To avoid this error call the constructor of the base class with appropriate arguments as required for the constructor available in the base class as shown below:
//Replace Arguments in base  with arguments as required for the 
//constructor of Decorator
protected ScrollDecorator(Decorator _decorator) : base(Arguments)
{
    this.decorator = _decorator;
}


您缺少默认构造函数
添加以下内容
you missing default constructor
add following
protected ScrollDecorator()
      {

      }


您好,

我认为发生此错误是因为您创建类的对象时未传递
之类的值
Hello,

i think this error occurred because you create object of the class without passing a value like
Test t = new Test();


和您的班级没有0参数构造函数

祝你好运
快乐编码


and your class don''t have 0 arguments constructor

Best Luck
Happy Coding


这篇关于对象不包含带有0个参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆