Reputation: 35
In the following program i am getting error like this Addtime.cpp: In function ‘int main()’: Addtime.cpp:41:6: error: expected ‘;’ before ‘time1’ I checked thoroughly the program, not able to find the error. please help me.
#include <iostream>
#include<string>
using namespace std;
class time
{
public:
int hours;
int minutes;
int seconds;
void showtime()
{
cout<<"time is "<<hours <<"hours:"<<minutes<<"minutes:"<<seconds<<"seconds \n";
}
time(int temph,int tempm,int temps)
{
hours=temph;
minutes=tempm;
seconds=temps;
}
time operator +(time t2)
{
int temph;
int tempm;
int temps;
temph = hours + t2.hours;
tempm = minutes + t2.seconds;
temps = seconds + t2.minutes;
tempm = (tempm + temps)/60;
temps = temps % 60;
temph = (temph + tempm)/60;
tempm = tempm % 60;
return time(temph,tempm,temps);
}
};
int main()
{
time time1 (12,15,15);
cout<<"the first value is";
time1.showtime();
time time2(10,30,30);
cout<<"the second value is";
time2.showtime();
time time3;
time3 = time1 + time2;
cout<<"the result is";
time3.showtime();
time time4;
time4 = time1 + time2 + time3;
time4.showtime();
return 0;
}
Upvotes: 0
Views: 558
Reputation: 26536
Here's a link to your snippet in an online compiler after I've fiddled with it a bit:
It looks like time
clashes with the time
function from the C standard library.
As such, the way to fix this is to either rename your class to something else (in the link I called it timer
), or to declare each instance with class time
to disambiguate the symbol.
Note that your variables time3
and time4
try to use the default constructor, which doesn't exist since you added a 3-argument constructor which suppresses the default, so neither of those will compile (I've commented them out in my example).
Upvotes: 2
Reputation: 50667
Actually it failed to compile at these lines that want to call the default constructor:
time time3;
....
time time4;
The reason is that if you write any other constructor manually (in your case: time(int,int,int)
), the compiler will no longer generate the default constructor for you.
To solve it, you will need to further add a default constructor for your time
class.
Upvotes: 2
Reputation: 638
add a default constructor time(void){/code/}; You need one if you declare an object like this time time4;
Upvotes: 0