Thread: Ambiguous call to function

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Ambiguous call to function

    This is a long program to sift through, but I am getting the following error:
    "Ambiguous call to overloaded function" Date:ate
    see the asterisks **** for the error location

    The program will accept a date in any format, and print that date in MM/DD/YY, standard, written and julian formats.

    Thanks very much for any help offered!

    Code:
    #include <iostream>
    #include <iomanip> // for setfill and setw
    #include <cstring> // for strings
    
    using namespace std;
    
    class Date 
    {
       public:
          Date( int = 1, int = 1, int = 1900 ); // default constructor
          Date( int ,int );  // constructor for DDD/YYYY 
          Date( const char* , int , int ); // constructor for January 15, YYYY
          void print() const;  // print date in month/day/year format
          void printJulian() const;  // print date in DDD YYYY format
          void printMDY() const;  // print date in MM/DD/YY
          void printLong() const;  // print date something like June 14, 2006
          ~Date();  // provided to confirm destruction order
    	     
       private:
          int month;
          int day;
          int year;
          int checkDay( int ) const; // function tests proper day
          int getMonthNumber( const char* ) const; 
    
    }; // end class Date; end of date1.h header file
    
       const char* monthTable[13] = {"","January","February","March","April","May","June","July","August","September","October","November","December"
    
    }; // global array
    
    
    // constructor confirms proper value for month; calls
    // utility function checkDay to confirm proper value for day
    Date::Date( int mn, int dy, int yr ) // this is stored in date1.cpp
    {
       if ( mn > 0 && mn <= 12 )  // validate the month
          month = mn;
    
       else {                     // invalid month set to 1
          month = 1;
          cout << "**WARNING: Month " << mn << " invalid. Set to month 1.\n";
       }
    
       year = yr;                 // should validate yr
       day = checkDay( dy );      // validate the day
    
    } // end Date constructor
    
    Date::Date(int _julian, int _year)
    {
       int calculator = 0;
    
       if ( _julian <= 31 )
          calculator = 1;
       if (_julian <= 59 )
          calculator = 2;
       if (_julian <= 90 )
          calculator = 3;
       if (_julian <= 120 )
          calculator = 4;
       if ( _julian <= 151 )
          calculator = 5;
       if (_julian <= 181 )
          calculator = 6;
       if (_julian <= 212 )
          calculator = 7;
       if (_julian <= 243 )
          calculator = 8;
       if ( _julian <= 273 )
          calculator = 9;
       if (_julian <= 304 )
          calculator = 10;
       if (_julian <= 334 )
          calculator = 11;
       if (_julian <= 365 )
          calculator = 12;
    
       cout << calculator << "/" << day << "/" << year; 
    }
    
    Date::Date(const char* _month, int _day, int _year) // untested
    {
       year = _year;                 // should validate yr
       month = getMonthNumber(_month);
       day = checkDay( _day );      // validate the day
    }
    
    void Date::print() const // prints as 3/12/1988
    {
       cout << month << '/' << day << '/' << year; 
    
    } // end function print
    
    void Date::printJulian() const // prints as DDD YYYY (ordinal/gregorian)
    {
       int calculator = 0;
    
       if (month == 1)
          calculator = 0;
       if (month == 2)
          calculator = 31;
       if (month == 3)
          calculator = 59;
       if (month == 4)
          calculator = 90;
       if (month == 5)
          calculator = 120;
       if (month == 6)
          calculator = 151;
       if (month == 7)
          calculator = 181;
       if (month == 8)
          calculator = 212;
       if (month == 9)
          calculator = 243;
       if (month == 10)
          calculator = 273;
       if (month == 11)
          calculator = 304;
       if (month == 12)
          calculator = 334;
    
       int gregorian;
       gregorian = (day + calculator);
    
       cout << gregorian << '/' << year; 
    
    } // end function printJulian
    
    void Date::printMDY() const // prints as MM/DD/YY
    {
       cout << setfill('0') << setw(2) << month << '/' << setw(2) << day << '/' << setw(2) << year; 
    
    } // end function print3
    
    void Date::printLong() const // prints, for example, March 12, 1988 - written format
    {
    	const char* monthArray[13] = {"null","January","February","March","April","May","June","July",
    		"August","September","October","November","December"};
    
        cout << monthArray[month] << " " << day << ", " << year; 
    
    } // end function print4
    
    // output Date object to show when its destructor is called
    Date::~Date()
    { 
    
    } // end ~Date destructor
    
    // utility function to confirm proper day value based on 
    // month and year; handles leap years, too
    int Date::checkDay( int testDay ) const
    {
       static const int daysPerMonth[ 13 ] = 
          { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
       // determine whether testDay is valid for specified month
       if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
          return testDay;
    
       // February 29 check for leap year 
       if ( month == 2 && testDay == 29 &&
          ( year % 400 == 0 ||                      
             ( year % 4 == 0 && year % 100 != 0 ) ) )
          return testDay;
    
       cout << "Day " << testDay << " invalid. Set to day 1.\n";
    
       return 1;  // leave object in consistent state if bad value
    
    } // end function checkDay; end of date1.cpp
    
    int Date::getMonthNumber(const char* _month) const
    {
       int key=0;
       for (int i = 1; i< 13;i++){
       if (!strcmp(_month,monthTable[i]))
          {
             key = i;
             break;
          }
       }
       return key;
    }
    
    
    int main()
    {
        Date d( 1, 25, 1974 ); // first date
        Date e( 02, 05, 1921 ); // second date, MM/DD/YY
        Date f( 256, 1993 ); // third date, Julian **** ambiguous call to overloaded function ****
        //Date g( January 12, 1994 ); // fourth date, written, untested
    
        cout << "First date entered is: " ;
        d.print();
        cout << "\nFirst date in DDD/YYYY (Gregorian) format: ";
        d.printJulian();
        cout << "\nFirst date in MM/DD/YY format: ";
        d.printMDY();
        cout << "\nFirst date in written format: ";
        d.printLong();
        cout << endl;
    
        cout << "\nSecond date entered is: " ;
        e.print();
        cout << "\nSecond date in DDD/YYYY (Gregorian) format: ";
        e.printJulian();
        cout << "\nSecond date in MM/DD/YY format: ";
        e.printMDY();
        cout << "\nSecond date in written format: ";
        e.printLong();
        cout << endl;
    
        cout << "\nThird date entered is: " ;
        f.print();
        cout << "\nThird date in DDD/YYYY (Gregorian) format: ";
        f.printJulian();
        cout << "\nThird date in MM/DD/YY format: ";
        f.printMDY();
        cout << "\nThird date in written format: ";
        f.printLong();
        cout << endl;
    
       return 0;
    
    } // end main
    THE redheaded stepchild.

  2. #2
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Ack, I'm trying to edit my post, but it's the
    Code:
    Date::Date
    function, not the smily.
    THE redheaded stepchild.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
          Date( int = 1, int = 1, int = 1900 ); // default constructor
          Date( int ,int );  // constructor for DDD/YYYY
    So which constructor gets called in response to Date(3,5)? Both are valid!

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The logic being, as Darryl is saying, is when you make a constructor with default arguements, any arguement that isn't passed uses the default. So calling Date(240,1982) would be valid with the default constructor resulting in the date of 240/1982/1900.

    This is why it's more proper to create your default constructor without arguements, like so:
    Code:
    class Date {
       Date() {month = day = 1; year = 1900; } // Default
       Date(int, int, int); // Your mm/dd/yyyy overloaded constructor
       Date(int,int); // Your ddd/yyyy overloaded constructor
       /*... the rest of the class ...*/
    Also, to fix your "Date::Date" problem, there is a checkbox below your post textbox the reads "Disable smileys in text". You want to click that one.
    Last edited by SlyMaelstrom; 04-05-2006 at 05:36 PM.
    Sent from my iPadŽ

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    better yet, use the type system, it's your friend.

    Code:
    Date(int Month, int Day, int Year);
    
    // I use this class as a normal human 
    Date d(31, 12, 2005); // you crazy americans with your mm/dd/yyyy!! :-)
    // oops no such date as 12th of month 31!!
    but I can use the type system to help me
    Code:
    struct Month
    {
        explicit Month(int m)
        : m_month(m) {}
        int m_month;
    
    };
    
    struct Day
    {
        explicit Day(int d)
        : m_day(d) {}
        int m_day;
    };
    
    struct Year
    {
        explicit Year(int y)
        : m_year(y) {}
        int m_year;
    };
    
    Date(const Month &m, const Day &d, int year);
    
    // now this is a compiler error
    Date(31, 12, 2004);
    
    // instead use
    Date(Month(12), Day(31), Year(2004));
    read the Zen of python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Funny, Chaos! Thank you everyone for your responses.
    THE redheaded stepchild.

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Although that way may not be obvious at first unless you're Dutch.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM