473,804 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constructor pain

Hi All,

why does this:

class Base {
protected:
int var;
public:
Base():var(0){} ;
virtual int getVar() = 0;
};

class Class1 : public Base{
protected:
int otherVar;
public:
Class1():otherV ar(1){};
Class1(int i = 0):otherVar(i){ };
int getOtherVar(){r eturn otherVar;}
int getVar(){return var;}
};

give me an error:
error: call of overloaded `Class1()' is ambiguous
note: candidates are: Class1::Class1( int)
note: Class1::Class1( )

I only have a single constructor that takes an int as a parameter, and a
default that takes no argument. Where is the ambiguity?

Thanks for your help.

Regards

Michael
Mar 10 '07 #1
3 1946
Michael wrote:
Hi All,

why does this:

class Base {
protected:
int var;
public:
Base():var(0){} ;
virtual int getVar() = 0;
};

class Class1 : public Base{
protected:
int otherVar;
public:
Class1():otherV ar(1){};
Class1(int i = 0):otherVar(i){ };
int getOtherVar(){r eturn otherVar;}
int getVar(){return var;}
};

give me an error:
error: call of overloaded `Class1()' is ambiguous
note: candidates are: Class1::Class1( int)
note: Class1::Class1( )

I only have a single constructor that takes an int as a parameter, and a
default that takes no argument. Where is the ambiguity?
Because your constructor with an int as a parameter also has a default
value. So when you supply no arguments to your constructor either one
could be called. Seems very straightforward to me.

Ask yourself, if you write

Class1 c;

what did you expect the value of c.otherVar to be? If you expected it to
be 0, then remove your first constructor; if you expected it to be 1,
then remove the default argument from your second constructor.

john
Mar 10 '07 #2
On 10 Mrz., 11:12, "Michael" <michael5...@ya hoo.comwrote:
class Class1 : public Base{
[...]
public:
Class1():otherV ar(1){}; #1
Class1(int i = 0):otherVar(i){ }; #2
[...]
};

give me an error:
error: call of overloaded `Class1()' is ambiguous
note: candidates are: Class1::Class1( int)
note: Class1::Class1( )

Where is the ambiguity?
You also give a default argument of 0 to the ctor #2. So there is the
ambiguity: shall Class1() calling the ctor #1 or #2 with i=0?

hth

Mar 10 '07 #3
Michael wrote:
Hi All,

why does this:

class Base {
protected:
int var;
public:
Base():var(0){} ;
virtual int getVar() = 0;
};

class Class1 : public Base{
protected:
int otherVar;
public:
Class1():otherV ar(1){};
Class1(int i = 0):otherVar(i){ };
int getOtherVar(){r eturn otherVar;}
int getVar(){return var;}
};

give me an error:
error: call of overloaded `Class1()' is ambiguous
note: candidates are: Class1::Class1( int)
note: Class1::Class1( )

I only have a single constructor that takes an int as a parameter,
and a default that takes no argument. Where is the ambiguity?
How is the compiler to know when you want the default parameter, and when
you want the constructor without a parameter?
Bo Persson
Mar 10 '07 #4

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

Similar topics

2
2890
by: theWizK | last post by:
Hello all. I have noticed that when I generate a strongly-typed dataset from an xml schema that the DataTables that are generated have their constructors marked as internal. What this means is when I try to instantiate one of the strongly-typed tables from this dataset from a different assembly, I cannot. Let me provide examples... If I have a simple dataset like this:
17
2518
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the following cases, shall a default constructor be provided? 1. Inheritance. Class A is derived from class B. Shall a default constructor
45
6370
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
19
6570
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do not declare one? 2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments? Thanks, Andrew
15
6024
by: Youssef Mesri | last post by:
What is the difference between these two situations: 1- hold a namespace which contains justs some functions: namespace MyNamespace { void foo1(); void foo2(); void foo3(); void foo4();
18
6525
by: Peter Gummer | last post by:
This is a design question. I have a project containing a dozen or so classes with protected internal constructors. My reason for having protected internal constructors is to prevent classes in other assemblies from instantiating them. There is a factory class that instantiates them. Only the factory should do so, because some of these classes have subclasses that will be created instead. The decision of which class to instantiate...
23
7240
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
7
2334
by: Blair Craft | last post by:
hi, I got a class have 2 constructors: static int g_idx_counter = 0; Object::Object():{ counter = g_idx_counter++; created_at = last_used = time(NULL);
5
1549
by: vulpes | last post by:
This similar situation will explain somewhat, what I want to do: class X { public: X() : map_(*this) {} bool operator() (int a, int b) { return goodStuff(a) < goodStuff(b); } int goodStuff(int a) { ... }
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.