Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructor pain

Reply
Thread Tools

constructor pain

 
 
Michael
Guest
Posts: n/a
 
      03-10-2007
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()therVar(1){};
Class1(int i = 0)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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-10-2007
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()therVar(1){};
> Class1(int i = 0)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
 
Reply With Quote
 
 
 
 
Marco Wahl
Guest
Posts: n/a
 
      03-10-2007
On 10 Mrz., 11:12, "Michael" <(E-Mail Removed)> wrote:

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

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      03-10-2007
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()therVar(1){};
> Class1(int i = 0)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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Chronic Back Pain Michael MCSD 0 04-02-2006 02:43 AM
Chronic Back Pain Michael MCSE 0 04-02-2006 02:43 AM
Modelsim error: Cannot read output pain Olaf Petzold VHDL 12 12-23-2005 09:15 PM
DropDownList Pain =?Utf-8?B?S1JN?= ASP .Net 2 05-25-2005 04:28 PM



Advertisments