t1, t2, t3 undefined

when i run the following code it is showing that t1, t2, t3 are undefined! But think they are object of class time. Then why is it showing that t1, t2, t3 are undefined?

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
 #include<iostream>
using namespace std;
class time
{
int hr, min, sec;
public:
    void get()
    {
        cout << "\nEnter Hour :: ";
        cin >> hr;
        cout << "\nEnter Minutes :: ";
        cin >> min;
        cout << "\nEnter Seconds :: ";
        cin >> sec;
    }
    void disp()
    {
        cout << "[ " << hr << ":" << min << ":" << sec << " ] \n";
    }
    void sum(time &, time &);
};
void time::sum(time &t1, time &t2)
{
    sec = t1.sec + t2.sec;
    min = sec / 60;
    sec = sec % 60;
    min = min + t1.min + t2.min;
    hr = min / 60;
    min = min % 60;
    hr = hr + t1.hr + t2.hr;
}
int main()
{
    time t1, t2, t3;
    cout << "\nEnter 1st time Details :: \n";
    t1.get();
    cout << "\nEnter 2nd time Details :: \n";
    t2.get();
    cout << "\nThe 1st time entered is :: ";
    t1.disp();
    cout << "\nThe 2nd time entered is :: ";
    t2.disp();
    t3.sum(t1, t2);
    cout << "\nThe Sum of two times are :: ";
    t3.disp();
    return 0;
}
I'm going to guess name-collision
the standard defines a time() function, so the compiler does not understand what are you referring to.
change the name of your class (and perhaps learn to use namespaces)
Because time is a standard c function. - which is probably being included at some point by iostream. Try:

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
#include <iostream>

class time1
{
	int hr {}, min {}, sec {};

public:
	void get()
	{
		std::cout << "\nEnter Hour :: ";
		std::cin >> hr;

		std::cout << "\nEnter Minutes :: ";
		std::cin >> min;

		std::cout << "\nEnter Seconds :: ";
		std::cin >> sec;
	}

	void disp()
	{
		std::cout << "[ " << hr << ":" << min << ":" << sec << " ] \n";
	}

	void sum(time1&, time1&);
};

void time1::sum(time1& t1, time1& t2)
{
	sec = t1.sec + t2.sec;
	min = sec / 60;
	sec = sec % 60;
	min = min + t1.min + t2.min;
	hr = min / 60;
	min = min % 60;
	hr = hr + t1.hr + t2.hr;
}

int main()
{
	time1 t1, t2, t3;

	std::cout << "\nEnter 1st time Details :: \n";
	t1.get();

	std::cout << "\nEnter 2nd time Details :: \n";
	t2.get();

	std::cout << "\nThe 1st time entered is :: ";
	t1.disp();

	std::cout << "\nThe 2nd time entered is :: ";
	t2.disp();

	t3.sum(t1, t2);

	std::cout << "\nThe Sum of two times are :: ";
	t3.disp();
	return 0;
}


Topic archived. No new replies allowed.