By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

constructor pain

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.