Date Template

This is a discussion on Date Template within the C++ Programming forums, part of the General Programming Boards category; Help, I have a Date Template where everything has gone wrong. I am not sure If I am understanding this ...

  1. #1
    Unregistered
    Guest

    Question Date Template

    Help, I have a Date Template where everything has gone wrong. I am not sure If I am understanding this correctly. My text tells me to create a regular function and then convert it to a template my Date class before when it was an int worked fine. Now that I have attempted to convert it to a Date Template -- I get a lot of error. Am I seeing this right. Eventually, I will need to use this date class template to work with a linked list. [That's a later question]

    Here is what I have. Please help

    **********************
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    //#include <fstream.h>

    //class template

    template <class T>

    class Date

    {
    private:
    T month;
    T day;
    T year;

    public:
    void setMonth(T);
    void setDay(T);
    void setYear(T);

    T getMonth(void);
    T getDay(void);
    T getYear(void);

    };

    void Date::setMonth(T m)
    {
    month = m;
    }
    void Date::setDay(T d)
    {
    day = d;
    }
    void Date::setYear(T y)
    {
    year = y;
    }

    T Date::getMonth(void)
    {
    return month;
    }
    T Date::getDay(void)
    {
    return day;
    }
    T Date::getYear(void)
    {
    return year;
    }


    void main(void)
    {
    Date format;
    T Mo, Dy, Year, i = 0, year = 0;
    char alphaMonth[12][10] = {"January", "February", "March", "April",
    "May", "June", "July", "August", "September",
    "October", "November", "December"};
    cout.precision(2);
    cout.setf(ios::fixed);
    cout << "This program calculates a date in different formats.";
    cout << endl;
    cout << endl;
    cout << "Enter the month number: ";
    cin >> Mo;
    while (Mo < 0 || Mo > 12)
    {
    cout << "Enter the numeric equivalent for month (1 to 12): ";
    cin >> Mo;
    }
    format.setMonth(Mo);
    cout << "Enter the day: ";
    cin >> Dy;
    while (Dy < 0 || Dy > 31)
    {
    cout << "Enter a day between (1 to 31): ";
    cin >> Dy;
    }
    format.setDay(Dy);
    cout << "Enter the year (XXXX): ";
    cin >> Year;
    cout << endl;
    format.setYear(Year);
    if(format.getYear() < 2000)
    {
    year = format.getYear() - 1900;
    }
    else if (format.getYear() > 1999)
    {
    year = format.getYear() - 2000;
    }

    cout << "Numeric date format: ";
    cout << ((format.getMonth() < 10) ? "0":"") << format.getMonth() << "/"
    <<((format.getDay() < 10) ? "0":"") << format.getDay() << "/"
    << ((year<10) ? "0":"") << year;
    cout << endl;
    i = format.getMonth() - 1;
    cout << endl;
    cout << "Standard date format: ";
    cout << alphaMonth[i] << " " << ((format.getDay() < 10) ? "0":"")
    << format.getDay() << ", " << format.getYear() << "\n";
    cout << endl;
    cout << "Alternate date format: ";
    cout << ((format.getDay() < 10) ? "0":"") << format.getDay()
    <<" " <<alphaMonth[i] << " " << format.getYear();
    cout << endl << endl;

    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    All your class member functions need to be modified to be templated functions, as an example:
    Code:
    template <class T> void Date::setMonth( T m )
    {
        month = m;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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