How can I convert a time object into another object using a conversion operator function?

Hello,

I have 2 classes, Time and Ltime. One has objects which are integers and the other as a long int. I have been tasked with performing a class to class conversion using a conversion operator function. I have written the function but have a number of errors. Not sure where i am going wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>

using namespace std;

class Ltime;
// class declaration section
class Time
{
    private:
        int hours, minutes, seconds;

    public:
        Time(int = 1, int = 1, int = 1); // constructor
        operator Ltime(); // conversion operator function
        void showTime();

};
// class implementation section
// constructor
Time::Time(int hr, int mins, int sec)
{
    hours = hr;
    minutes = mins;
    seconds = sec;
}

    // conversion operator function converting from Time to long
Time::operator Ltime() // must return a long
{
    long elapsedsec;
    elapsedsec = (hours * 3600) + (minutes * 60) + seconds;
    return(elapsedsec);
}

// member function to display a Time
void Time::showTime()
{
    cout    << setfill('0')
            << setw(2) << hours << ':'
            << setw(2) << minutes << ':'
            << setw(2) << seconds;
    return;
}

class Ltime
{
    private:
        long elsecs;

    public:
        Ltime(int = 0);
        void showLtime();
};

Ltime::Ltime(long es)
{
    elsecs = es;
}

void Ltime::showLtime()
{
    cout << "The time as a long integer is " << elsecs <<endl;
}

int main()
{
    Time a(4,1,10); // declare and initialize one object of type Time
    operator Ltime(a);

    cout << "a's Time is ";
    a.showTime();
    cout << "\nThis Time, as a long integer, is " << b << endl;

    return 0;
}
> I have written the function but have a number of errors.
¿is it too much to ask to write the error messages verbatim?
|| foo.cpp: In member function ‘Time::operator Ltime()’:
foo.cpp|29 col 22| error: return type ‘class Ltime’ is incomplete
||  Time::operator Ltime() // must return a long
||                       ^
|| foo.cpp: At global scope:
foo.cpp|56 col 1| error: prototype for ‘Ltime::Ltime(long int)’ does not match any in class ‘Ltime’
||  Ltime::Ltime(long es)
||  ^~~~~
foo.cpp|46 col 7| error: candidates are: constexpr Ltime::Ltime(Ltime&&)
||  class Ltime
||        ^~~~~
foo.cpp|46 col 7| error:                 constexpr Ltime::Ltime(const Ltime&)
foo.cpp|52 col 9| error:                 Ltime::Ltime(int)
||          Ltime(int = 0);
||          ^~~~~
|| foo.cpp: In function ‘int main()’:
foo.cpp|69 col 5| error: ‘operator Ltime’ not defined
||      operator Ltime(a);
||      ^~~~~~~~
foo.cpp|73 col 54| error: ‘b’ was not declared in this scope
||      cout << "\nThis Time, as a long integer, is " << b << endl;
||                                                       ^


> 29: error: return type ‘class Ltime’ is incomplete
you are trying to use Ltime before its definition (line 46--54)
Move the definition above line 29


> 56: error: prototype for ‘Ltime::Ltime(long int)’ does not match any in class ‘Ltime’
long is not int
decide which one you'll use


> 69 error: ‘operator Ltime’ not defined
> operator Ltime(a);

static_cast<Ltime>(a);


> 73 error: ‘b’ was not declared in this scope
no idea where this `b' came from.
Last edited on
also consider using the 'explicit' keyword in conversion functions:
https://msdn.microsoft.com/en-us/library/wwywka61.aspx#The%20explicit%20keyword%20and%20problems%20with%20implicit%20conversion
and const qualify your code wherever possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std;

class Ltime
{
    private:
        long elsecs;

    public:
        Ltime(const long es = 0);
        void showLtime()const;
};

Ltime::Ltime(const long es)
    : elsecs(es){}

void Ltime::showLtime()const
{
    cout << "The time as a long integer is " << elsecs <<endl;
}
class Time
{
    private:
        int hours, minutes, seconds;

    public:
        Time(int = 1, int = 1, int = 1); // constructor
        explicit operator Ltime()const; // conversion operator function
};

Time::Time(const int hr, const int mins, const int sec)
 : hours(hr), minutes (mins), seconds (sec){}


Time::  operator Ltime() const // must return a long
{
    long elapsedsec;
    elapsedsec = (hours * 3600) + (minutes * 60) + seconds;
    return(elapsedsec);
}

int main()
{
    Time a(4,1,10); // declare and initialize one object of type Time
    auto b = static_cast<Ltime>(a);

    b.showLtime();
}
Hello,

Thanks for your combined tips! @gunnerfunner I am not sure of some of your syntax. I'm new at this C++ stuff and working from an old textbook in my spare time. Here is my code with the improvements you both suggested. I'm sorry for not displaying the notifications from the compiler. That makes things a lot easier to help if it's there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>

using namespace std;

class Ltime;
// class declaration section

class Ltime
{
    private:
        long elsecs;

    public:
        Ltime(long = 1);
        void showLtime();
};

class Time
{
    private:
        int hours, minutes, seconds;

    public:
        Time(int = 1, int = 1, int = 1); // constructor
        operator Ltime(); // conversion operator function
        void showTime();

};
// class implementation section
// constructor
Time::Time(int hr, int mins, int sec)
{
    hours = hr;
    minutes = mins;
    seconds = sec;
}

    // conversion operator function converting from Time to long
Time::operator Ltime() // must return a long
{
    long elapsedsec;
    elapsedsec = (hours * 3600) + (minutes * 60) + seconds;
    return(elapsedsec);
}

// member function to display a Time
void Time::showTime()
{
    cout    << setfill('0')
            << setw(2) << hours << ':'
            << setw(2) << minutes << ':'
            << setw(2) << seconds;
    return;
}

Ltime::Ltime(long es)
{
    elsecs = es;
}

void Ltime::showLtime()
{
    cout << elsecs <<endl;
}

int main()
{
    Time a(10,10,10); // declare and initialize one object of type Time
    Ltime b;

    b = static_cast <Ltime> (a);

    cout << "a's Time is ";

    a.showTime();

    cout << "\nThis Time, as a long integer, is "<< endl;

    b.showLtime();

    return 0;
}
1
2
// operator Ltime(); // conversion operator function
operator Ltime() const ; // make it const (both declaration and definition) 

See: 'What is a “const member function”?' https://isocpp.org/wiki/faq/const-correctness
Topic archived. No new replies allowed.