Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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()
       {
 
       }
    }
Posted 3-May-12 0:55am
Comments
Wes Aday at 3-May-12 7:31am
   
You are trying to instantiate an instance of the Decorator class that does not have a 0 argument constructor. What is so confusing about that?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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;
}
  Permalink  
Comments
Espen Harlinn at 3-May-12 8:45am
   
5'ed!
VJ Reddy at 3-May-12 8:49am
   
Thank you, Espen.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you missing default constructor
add following
  protected ScrollDecorator()
        {
            
        }
  Permalink  
Comments
Anele.Ngqandu at 3-May-12 7:05am
   
Well Sir I tried that but still nothing thats why am confused by this
Prasad_Kulkarni at 3-May-12 7:23am
   
Countered
Sangramsingh Pawar at 3-May-12 7:31am
   
I think you missing to call base class constructor. It seems that base class has no default constructor, therefore u callbase class constructer that takes arguments
Anele.Ngqandu at 3-May-12 7:39am
   
thats true Sangramsingh
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

type conflict
 
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()
       {
 
       }
    }
also add below constructor
 
ScrollDecorator()
        {
            
        }
 
  Permalink  
Comments
Anele.Ngqandu at 3-May-12 7:14am
   
I have to do the inheritance so cant do that...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello,
 
i think this error occurred because you create object of the class without passing a value like
Test t = new Test();
and your class don't have 0 arguments constructor
 
Best Luck
Happy Coding
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You are inheriting from a class and having the same class as instance member - wierd????
 
anyway looks like you base class has no default constructor and you will have to take the arguments that base class is expecting as derived class arguments and then pass them to the base class constructor as base(args) in derived class constructor.
 
protected ScrollDecorator(Decorator _decorator, args) : base(args)
{
    this.decorator = _decorator;
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 432
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,472
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100