Problem of Creating Two Constructors in a Class

This is a discussion on Problem of Creating Two Constructors in a Class within the C++ Programming forums, part of the General Programming Boards category; In most other classes I have written, I have no problem in calling a second constructor. But I don't know ...

  1. #1
    Yin
    Guest

    Question Problem of Creating Two Constructors in a Class

    In most other classes I have written, I have no problem in calling a second constructor. But I don't know why the following program that I wrote will always generate error when compiling:

    ------------------------------------------------------------------------------------

    #include <iostream>
    #include <stdlib.h>
    #include <string.h>


    class myDateDiff {
    private:
    int day;

    public:
    myDateDiff(void) { day = 0;}
    myDateDiff(const int i_day = 0) {
    set(i_day);
    }
    // Destructor defaults
    // Copy constructor defaults
    // Assignment operator defaults
    void set(const int i_day = 0) {
    day = i_day;
    }
    };

    int main () {
    myDateDiff df3;
    return (0);
    }

    ------------------------------------------------------------------------------------

    The error message in that:

    In function `int main()':
    call of overloaded `myDateDiff()' is ambiguous
    candidates are: myDateDiff::myDateDiff()
    myDateDiff::myDateDiff(int)

    Can anyone help? Thanks.

  2. #2
    Yin
    Guest

    Exclamation

    I have written another program which is of very similar structure but generates NO ERROR MESSAGE when compiling, isn't it a mystery?

    __________________________________________________

    #include <iostream>
    #include <stdlib.h>
    #include <string.h>

    class myDate {
    private:

    short int dd, mm, yyyy;

    public:

    myDate(void) { dd = mm = yyyy =0;}

    myDate (
    const short int i_dd,
    const short int i_mm,
    const short int i_yyyy = 0
    ) {
    set(i_dd, i_mm, i_yyyy);
    }
    // Destructor defaults
    // Copy constructor defaults
    // Assignment operator defaults
    void set(
    const short int i_dd,
    const short int i_mm,
    const short int i_yyyy = 0
    ) {
    dd = i_dd;
    mm = i_mm;
    yyyy = i_yyyy;
    }
    };

    int main() {
    myDate date3;
    return (0);
    }

    __________________________________________________

    Thanks.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    There is no difference in the signature between

    func(void)

    and

    func(int a=2)

    when calling func with no arguments. That was the error.. the compiler doesn't know which function to call, because each are equally appriopriate.


    There is a difference, however, between

    func(void)

    and

    func(int a, int b, int c = 0)

    because if you specify 2 or 3 args, the latter is called, while if you have no arguments, the former is called.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    39

    parameters

    default constructor dont have parameters and argument so stop putting void in it.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To fix this take the default argument out of the constructor with the int argument as a single parameter. If you don't passing anything to the constructor how does the compiler know whether to use the default constructor or the constructor with the single argument in default mode?

    Alternatively, you could consider assigning day the value of 2 in the default constructor such that day will have the default value of 2 irrespective of whether the default constructor or the single parameter constructor in default mode was used, (although I doubt the compiler will be smart enough to know that it doesn't matter which constructor was used and it will probably still view the scenario as ambiguous).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 02:01 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 04:05 PM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21