473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Base class does not contain a constructor that takes '0' argument

I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
Nov 6 '08 #1
6 47644
Author wrote:
I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
C# enforces that construction of the base class must be properly completed
before the derived class constructor is executed. Because your derived
constructor doesn't explicitly call a base class constructor, the compiler
automatically inserts a call to the constructor without arguments. But
there's no such constructor, so it fails.

To fix this, you must make a constructor available for the derived class to
call. In this case, initializing the properties does not seem to be
mandatory (since users can pass null for both, and even unset them later) so
there's no problem making a public parameterless constructor available. In
the general case, you could make a protected constructor available
specifically for derived classes to initialize the base state with.

--
J.
Nov 6 '08 #2


Author wrote:
I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
Let's say you create an object of SubClass like this
SubClass sc = new SubClass(1,2);

Now, when subclass' object is created, it's base class object is also
created internally. So, it's constructor also must be invoked. Now, the
derived class is not explicitly specifying which constructor to use for
the base class, in which case the compiler searches for a constructor
with 0 parameters.

So, to solve this error, you can either add a default constructor to
BaseClass or do something like this

class SubClass : BaseClass
{
public SubClass(int int1, int int2):base("","")
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}
Nov 6 '08 #3
Author,

The compiler basically assumes this when you write your constructor:

class SubClass : BaseClass
{
public SubClass(int int1, int int2) : base()
{

Since you have defined only one constructor in BaseClass and it takes
arguments, that's where the error comes from. Basically, you need to
explicitly call the constructor on the base class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Author" <gn********@gmail.comwrote in message
news:94**********************************@h23g2000 prf.googlegroups.com...
>I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.

Nov 6 '08 #4
Many thanks to both of you.

Actually, I did notice that if I call the base class's contructor
using :base(blah blah), it error goes away.

So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?
Nov 6 '08 #5
Author wrote:
So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?
Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.

--
J.
Nov 6 '08 #6
On Nov 6, 2:38*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Author wrote:
So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?

Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.

--
J.
Perfecto! Gotcha. Thx.
Nov 6 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
10
by: Andrzej Kaczmarczyk | last post by:
Hi C# gurus :) I have a conversion problem. I have a class that I can't change - it's a DataColumn class; I have a wrapper class around it. public MyDataColumn : DataColumn {
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
1
by: Martijn Mulder | last post by:
Documentation suggests that I should make a call to the base-class method for some methods. When I try to do so for the class below, I get wrong results. What is the correct way to make a call to...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
1
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; ...
12
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.