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

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():otherVar(1){};
Class1(int i = 0):otherVar(i){};
int getOtherVar(){return 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 1929
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():otherVar(1){};
Class1(int i = 0):otherVar(i){};
int getOtherVar(){return 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...@yahoo.comwrote:
class Class1 : public Base{
[...]
public:
Class1():otherVar(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():otherVar(1){};
Class1(int i = 0):otherVar(i){};
int getOtherVar(){return 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
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...
17
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...
45
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...
19
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...
15
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
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...
23
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
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
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.