473,406 Members | 2,698 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,406 software developers and data experts.

Abstract Base Constructor Question

Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"

Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy
Nov 15 '05 #1
6 8520

"Randy" <rb***@sumaria.net> wrote in message
news:md*******************@nwrdny03.gnilink.net...
Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"
basically
class mySubClass : myBase
{
mySubClass() : base(10) {}
}

in other words, you have to specifiy a value for the base constructor. Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy

Nov 15 '05 #2
You're derived class needs to call the base classes constructor which
requires one parameter of type int. You need to modify the constructor of
the derived class to look like

mySubClass() : base(<some hard coded integer value here>) {}
or
mySubClass(int x) : base(x) {}

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
"Randy" <rb***@sumaria.net> wrote in message
news:md*******************@nwrdny03.gnilink.net...
Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"

Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy

Nov 15 '05 #3
In myBase, the only constructor you have takes a single argument. In
mySubClass, the only constructor takes no arguments. For inheritance to
work, the sub classed constructor must somehow call the base class
constructor. if there's no argument to pass, it doesn't know how.

If you change mySubClass to:

class mySubClass : myBase
{
mySubClass(int x) {}
}

that will work.

Optionally, you can specify which base class constructor to call and do
something like:

class mySubClass : myBase
{
mySubClass() : base(4) {}
}

In this case, we're passing the constant integer 4 to the base class
constructor.
Pete

--
http://www.petedavis.net
"Randy" <rb***@sumaria.net> wrote in message
news:md*******************@nwrdny03.gnilink.net...
Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"

Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy

Nov 15 '05 #4
Yes. You need to pass the constructor parameters up to the parent class
constructor, like so:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass(int x): base(x) {}
}

Hope this helps!

--
----
Doug Erickson [MSFT], Platform SDK UA
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2003 Microsoft Corporation. All rights
reserved.
"Randy" <rb***@sumaria.net> wrote in message
news:md*******************@nwrdny03.gnilink.net...
Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"

Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy

Nov 15 '05 #5
Randy,

In addition to what the others said, you also have to make the base
constructor accessible to the derived class. It's currently private,
so it can't be used by mySubClass. Make it at least protected.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #6
mySubClass():base(4) {} works! Many thanks to all.

Randy

"Randy" <rb***@sumaria.net> wrote in message
news:md*******************@nwrdny03.gnilink.net...
Problem reduced to simplist form:

abstract class myBase
{
myBase(int x)
{
Console.Writeline("x = {0}", x);
}
}
class mySubClass : myBase
{
mySubClass() {}
}

Yields the following error: "No overload for method 'myBase' takes '0'
arguments"

Any ideas on what I have to do to mySubClass to get this to compile?

Thanks,
Randy

Nov 15 '05 #7

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

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
1
by: Tony Johansson | last post by:
Hello!! Assume you have two classes one class called Base which is an abstract class and one derived class called Derived. You are not allowed to create an object of class Base like new...
9
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
2
by: talkingpidgin | last post by:
I am trying to figure out why it is not conventional to use protected constructors in abstract classes since the only time they should be called is by the constructors of it's derived classes. Is...
2
by: ali | last post by:
Hi, I have a question with regards to abstract classes (understanding that I cannot instantiate an abstract class). If my Abstract base class has: private: int width; int height;
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.