Question

I'm teaching myself C++ and learning about class to class conversions. I understand that when trying to do a class to class conversion, you must use the operator keyword. In this particular case, I have 2 classes: Ltime and Time. I am trying to create a method using operator to be able to convert each class to each other.

My problem is that because I am trying to use the operator keyword to convert from a class, the first class declaration will be referring to a class that is not yet defined. If I was to switch the first class and second class, the same problem would persist since they both depend on each other.

My textbook says to solve this problem, you use a forward declaration for the second class. I did this but this does not solve my problem. I then tried to split up the classes into separate files for each and use include statements in each and in the main file but that did not work. If this is the solution, could someone please explain the structure of this solution?

Can someone please explain how I could get this code to work? For clarity, below is the problem and my code. Thank you in advance.

Write a C++ program that has a Time class and an Ltime class. The Time class should have integer data members named hours, minutes, and seconds, and the Ltime class should have a long integer data member named elsecs, which represents the number of elapsed seconds since midnight. For the Time class, include a conversion operator function named Ltime() that converts a Time object to an Ltime object. For the Ltime class, include a conversion operator function named Time() that converts an Ltime object to a Time object.

My code:

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

/*
 * Pg 527) §11.3)
 */


class Ltime;

class Time
{
private:
    int hours;
    int minutes;
    int seconds;
public:
    Time(int hr=1, int min=2, int sec=3) : hours(hr), minutes(min), seconds(sec) {}
    void show();
    operator Ltime();
};

void Time::show()
{
    cout << setfill('0')
         << setw(2) << hours << " : "
         << setw(2) << minutes << " : "
         << setw(2) << seconds;
}

Time::operator Ltime()
{
    return Ltime(3600*hours + 60*minutes + seconds);
}


class Ltime
{
private:
    long secondsFromMidnight;
public:
    Ltime(long = 0);
    void show();
    operator Time();
};

Ltime::Ltime(long sec)
{
    secondsFromMidnight = sec;
}

void Ltime::show()
{
    cout << secondsFromMidnight;
}

Ltime::operator Time()
{
    int hours, minutes, seconds;
    hours = int(3600/secondsFromMidnight);
    minutes = (secondsFromMidnight / 60)%60;
    seconds = secondsFromMidnight%60;

    return Time(hours, minutes, seconds);
}




int main()
{
    Time a;
    a.show();
    cout << endl;

    Time b(1,0,0);
    b.show();
    cout << endl;

    Ltime c;
    c = Ltime(b);
    c.show();
    cout << endl;

    Ltime(b).show();
    cout << endl;

    return 0;
}
Was it helpful?

Solution

I ran into this problem before. You were correct in forward declaring the class, but you attempt to use an incomplete type. The solution is to move Time::operator Ltime() after the definition of Ltime.

// ...snip...

void Ltime::show()
{
    cout << secondsFromMidnight;
}

Time::operator Ltime()
{
    return Ltime(3600*hours + 60*minutes + seconds);
}

Live Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top