Question

I'm teaching myself how to program in C++ and working on this problem:

Write a C++ program that has a Date class and a Julian class. The Julian class should represent a date as a long integer. For this program, include a conversion operator function in the Date class that converts a Date object to a Julian object, using the algorithm provided. Test your program by converting 1/31/2011 and 3/16/2012, which correspond to the Julian dates 734533 and 734943.

So we have to have a Date method which converts an argument to a Julian class. I understand that this must be done through the keyword operator. I wrote some code and get the following error message:

ttt.cpp:34:7: error: incomplete result type 'Julian' in function definition
Date::operator Julian()
      ^
ttt.cpp:11:7: note: forward declaration of 'Julian'
class Julian;   // Forward declaration
      ^
ttt.cpp:50:12: error: 'Julian' is an incomplete type
    return Julian(long(365*year + 31*(month-1) + day + T - MP));
           ^
ttt.cpp:11:7: note: forward declaration of 'Julian'
class Julian;   // Forward declaration
      ^
2 errors generated.

I am unclear what this error message means. I included a forward declaration because Julian is defined after Date. My code is below. I would appreciate any help. Thank you.

#include <iostream>
#include <iomanip>
using namespace std;

/*
 * Class to class conversion
 */


// CLASS DECLARATIONS=========================================================
class Julian;   // Forward declaration

// "Date" Class Declaration------------------------------------------
class Date
{
private:
    int month;
    int day;
    int year;
public:
    Date(int=7, int=4, int=2012);   // Constructor
    operator Julian();              // Method to convert "Date" class to "Julian"
    void showDate();                // print "Date"
};

// "Date" CLASS IMPLEMENTATION----------------------------
Date::Date(int mm, int dd, int yyyy)
{   // Constructor Method
    month = mm;
    day = dd;
    year = yyyy;
}

Date::operator Julian()
{   // Method to convert "Date" class to "Julian"
    int MP, YP, T;

    if( month <=2 )
    {
        MP = 0;
        YP = year - 1;
    }
    else
    {
        MP = int(0.4*month + 2.3);
        YP = year;
    }
    T = int(YP/4) - int(YP/100) + int(YP/400);

    return Julian(long(365*year + 31*(month-1) + day + T - MP));
}

void Date::showDate()
{
    cout << setfill('0')
         << setw(2) << month << '/'
         << setw(2) << day << '/'
         << setw(2) << year % 100;
}

// "Julian" CLASS DECLARATION--------------------------------------------------------
class Julian
{
private:
    int days;
public:
    Julian(long=0);         // Constructor
    void show();            // Print julian date
};

// "Julian" Class Implementation----------------------------------------------------
Julian::Julian(long d)
{
    days = d;
}


void Julian::show()
{
    cout << days << endl;
}




int main()
{
    Date a(1,31,2011);
    Date b(3,16,2012);

    Julian c, d;

    c = Julian(a);
    d = Julian(b);

    a.showDate();
    c.show();
    cout << endl;

    b.showDate();
    d.show();
    cout << endl;

    return 0;
}
Était-ce utile?

La solution

You need to define Julian class before Date class. Just the forward declaration won't work here because Date class needs complete definition of Julian class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top