Class Time

closed account (jvqpDjzh)
//Why this s***** program is not working?
//ERRORS

||=== Build: Debug in class_Time (compiler: GNU GCC Compiler) ===|
obj\Debug\Time.o||In function `ZN4TimeC2Ev':|
C:\Users\User1\Desktop\code__blocks\class_Time\Time.cpp|15|undefined reference to `vtable for Time'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


//main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
main.cpp
*/

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include "Time.h"

int main(int argc, char** argv)
{
    Time* t1 = new Time();

    //cin.get();
    return 0;
}


//Time.h
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
/*
Time.h
*/

#ifndef TIME_H
#define TIME_H

class Time
{
    public:
        Time();
        //Time(int, int, int);

        virtual ~Time();
        void setTime(int, int, int);
        void printUniversal();//print the time in universal format
        void printStandard();

    protected:
    private:
        int hour;//from 0-23
        int minute;//0-59
        int second;//0-59
};

#endif // TIME_H 


//Time.cpp
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
/*
Time.cpp
definition of the member functions of the Time class
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setfill;
using std::setw;

#include "Time.h"

Time::Time()//ERROR HERE!
{
    hour = minute = second = 0;
}

/*
//second constructor
Time::Time(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}
*/

void Time::setTime(int h, int m, int s)
{
    //the assignements are done if just the validation returns true
    hour = (h >= 0) && (h < 24)? h : 0;
    minute = (h >= 0) && (m < 60)? m : 0;
    second = (s >= 0) && (s < 60)? s : 0;
}

void Time::printUniversal()
{
    cout << setfill('0') << setw(2) << hour << ":" << setw(2)
         << minute << ":" << setw(2) << second << ":";
}
Last edited on
You declared a virtual destructor for Time, but never implemented it.
That explains the linker error.

At line16 of time.cpp you say there is an error. What is the error?
closed account (jvqpDjzh)
//Why can I call the the first constructor?
//ERROR FROM THE COMPILER: call of the overloaded Time() is ambigous

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
main.cpp
*/

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include "Time.h"

int main(int argc, char** argv)
{
    Time* t1 = new Time();

    //cin.get();
    return 0;
}



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
/*
Time.h
*/

#ifndef TIME_H//directives to the preprocessor
#define TIME_H

class Time
{
    public:
        Time();
        Time(int=0, int=0, int=0);

        virtual ~Time();
        void setTime(int, int, int);
        void printUniversal();//print the time in universal format
        void printStandard();

    protected:
    private://each public member should be private, except in an clearly demonstrable case: this the "Principle of least privilege"
        int hour;//from 0-23
        int minute;//0-59
        int second;//0-59
};

#endif // TIME_H 



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
/*
Time.cpp
definition of the member functions of the Time class
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setfill;
using std::setw;

#include "Time.h"

//first constructor
Time::Time()
{
    hour = minute = second = 0;
}

//second constructor
Time::Time(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}

Time::~Time() {}

void Time::setTime(int h, int m, int s)
{
    //the assignements are done if just the validation returns true
    hour = (h >= 0) && (h < 24)? h : 0;
    minute = (h >= 0) && (m < 60)? m : 0;
    second = (s >= 0) && (s < 60)? s : 0;
}

void Time::printUniversal()
{
    cout << setfill('0') << setw(2) << hour << ":" << setw(2)
         << minute << ":" << setw(2) << second << ":";
}

void Time::printStandard()
{
    cout << ( (hour == 0 || hour == 12)? 12 : hour % 12 ) << ":" << setfill('0') << setw(2)
         << minute << ":" << setw(2) << second << (hour < 12? " AM": " PM");
}
Last edited on
time.h lines 11 and 12 are ambiguous.

If you specify Time(), which constructor is supposed to be invoked? The default constructor, or the constructor with all arguments defaults?
closed account (jvqpDjzh)
I found out why: the constructor with default arguments had become the default constructor and a class can just have 1 default costructor, which isn't the case!
Thanks anyway!
Topic archived. No new replies allowed.