constructor pain

Discussion in 'C++' started by Michael, Mar 10, 2007.

  1. Michael

    Michael Guest

    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
    Michael, Mar 10, 2007
    #1
    1. Advertising

  2. 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():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
    John Harrison, Mar 10, 2007
    #2
    1. Advertising

  3. Michael

    Marco Wahl Guest

    On 10 Mrz., 11:12, "Michael" <> wrote:

    > 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
    Marco Wahl, Mar 10, 2007
    #3
  4. Michael

    Bo Persson Guest

    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():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
    Bo Persson, Mar 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold
    Replies:
    12
    Views:
    7,384
    Rob Dekker
    Dec 23, 2005
  2. =?Utf-8?B?S1JN?=

    DropDownList Pain

    =?Utf-8?B?S1JN?=, May 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    374
    =?Utf-8?B?S1JN?=
    May 25, 2005
  3. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    644
  4. Brent White

    Sorry to be such a pain

    Brent White, Oct 24, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    533
    Juan T. Llibre
    Oct 25, 2005
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,204
Loading...

Share This Page