Oct 10, 2020 at 10:50pm UTC
Hello, Im trying to call a function from my .h file, but i got an "no match for operator++" error.
Here's where the error occured :
1 2 3
AddMinute(++T1, 1);
cout<<isTimeValid(T1)<<endl;;
DisplayTime(T1);
Full code :
main.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
#include <iostream>
#include "time.h"
using namespace std;
int main(int argc, char ** argv) {
time T1;
setHour(&T1, 5);
setMinute(&T1, 30);
setSecond(&T1, 12);
cout<<isHourValid(T1)<<endl;
cout<<isMinuteValid(T1)<<endl;
cout<<isSecondValid(T1)<<endl;
cout<<isTimeValid(T1)<<endl;
DisplayTime(T1);
time T2;
setHour(&T2, 15);
setMinute(&T2, 62);
setSecond(&T2, 12);
cout<<isHourValid(T2)<<endl;
cout<<isMinuteValid(T2)<<endl;
cout<<isSecondValid(T2)<<endl;
DisplayTime(T2);
//change the minute of T2
setMinute(&T2, 43);
cout<<isTimeValid(T2);
DisplayTime(T2);
AddMinute(++T1, 1);
cout<<isTimeValid(T1)<<endl;;
DisplayTime(T1);
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 27
#ifndef TIME_H
#define TIME_H
struct time{
int hour;
int minute;
int second;
};
bool isHourValid(time T);
bool isMinuteValid(time T);
bool isSecondValid(time T);
bool isTimeValid(time T);
void AddMinute (time T1, int m);
void AddSecond (time T1, int s);
void MinusMinute (time T1, int m);
void MinusSecond (time T1, int s);
void setHour (time *T, int h);
void setMinute (time *T, int m);
void setSecond (time *T, int s);
void DisplayTime (time T);
#endif
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 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include <iostream>
#include "time.h"
using namespace std;
bool isHourValid(time T)
{
if (T.hour >= 0 && T.hour <= 23){
return true ;
}else {
return false ;
}
}
bool isMinuteValid(time T)
{
if (T.minute >= 0 && T.minute <= 59){
return true ;
}else {
return false ;
}
}
bool isSecondValid(time T)
{
if (T.second >= 0 && T.second <= 59){
return true ;
}else {
return false ;
}
}
bool isTimeValid(time T)
{
if (isHourValid(T) && isMinuteValid(T) && isSecondValid(T)){
return true ;
}
}
void AddMinute (time T1, int m)
{
++m;
if (T1.minute ==60){
++T1.hour;
T1.minute = 0;
}
}
void AddSecond (time T1, int s)
{
++s;
if (T1.minute >= 60){
++T1.hour;
T1.minute -= 60;
}
if (T1.second >= 60){
++T1.minute;
T1.second -= 60;
}
}
void MinusMinute(time T1, int m)
{
--m;
if (T1.minute >= 0){
--T1.hour;
T1.minute += 59;
}
if (T1.second >= 0){
--T1.minute;
T1.second += 59;
}
}
void MinusSecond(time T1, int s)
{
--s;
if (T1.second >= 0){
--T1.minute;
T1.second += 59;
}
if (T1.minute >= 0){
--T1.hour;
T1.minute += 59;
}
}
void setHour (time *T, int h)
{
T->hour = h;
}
void setMinute (time *T, int m)
{
T->minute = m;
}
void setSecond (time *T, int s)
{
T->second = s;
}
void DisplayTime (time T)
{
cout<<T.hour<<":" <<T.minute<<":" <<T.second<<endl;
}
}
Last edited on Oct 10, 2020 at 10:50pm UTC
Oct 10, 2020 at 11:15pm UTC
you do not have operator ++ on your 'time' struct, but you attempt ++T1 in your line 1 first section example.
you need to define ++ for the type you created in order to use it, or, instead of ++, before line 1 you need something like T1.second++;