constructor pain

M

Michael

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():eek:therVar(1){};
Class1(int i = 0):eek:therVar(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
 
J

John Harrison

Michael said:
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():eek:therVar(1){};
Class1(int i = 0):eek:therVar(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
 
M

Marco Wahl

class Class1 : public Base{
[...]
public:
Class1():eek:therVar(1){}; #1
Class1(int i = 0):eek:therVar(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
 
B

Bo Persson

Michael said:
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():eek:therVar(1){};
Class1(int i = 0):eek:therVar(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top