Hey if anyone could help that would be awesome. Its almost done but I keep getting this warning "C4715 ' Time::operator<' :not all control paths return a value" and I can't figure out what is causing it. The program works until the two time equal each other. Any suggestions would be appreciated. Thanks for the help
Time.h
class Time
{
private:
int hours, minutes, seconds;
public:
Time();
void Set (int h, int m, int s);
bool operator==(Time otherTime) const;
bool operator < (Time otherTime) const;
bool operator > (Time otherTime) const;
};
Main.cpp
#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
Time t1, otherTime;
int h, m, s;
cout<<"Input hour, minute and second: ";
cin>>h>>m>>s;
t1.Set(h, m, s);
cout<<"Input hour, minute and second: ";
cin>>h>>m>>s;
otherTime.Set(h, m, s);
if (t1 == otherTime)
cout<<"t1 equals to t2"<<endl;
if (t1 < otherTime)
cout<<"t1 is earlier than t2"<<endl;
if (t1 > otherTime)
cout<<"t1 is later than t2"<<endl;
}
Time.cpp(where my functions are and where the problem lies. According to the compiler the problem is in the very last function but no matter what I try nothing seems to work)
#include<iostream>
#include "Time.h"
using namespace std;
Time::Time()
{
int hours=0;
int minutes=0;
int seconds=0;
}
void Time::Set(int h, int m, int s)
{
hours=h;
minutes=m;
seconds=s;
}
Doesn't fix the warning but should correct the program for equal times.
1 2 3 4 5 6 7
bool Time::operator >(Time otherTime) const
{
if ((*this<otherTime) or (*this==otherTime)) // since *this<otherTime is false for equal times
returnfalse;
elsereturntrue;
}
Simple, you're code operator < code doesn't have a return statement for EVERY path in the code. Every if statement splits the path up. Let me write it out:
bool Time::operator <(Time otherTime) const
{
if (hours<otherTime.hours)
returntrue;
if (hours > otherTime.hours)
returnfalse;
if (hours==otherTime.hours)
{
if (minutes < otherTime.minutes)
returntrue;
if (minutes > otherTime.minutes)
returnfalse;
if (minutes== otherTime.minutes)
{
if (seconds< otherTime.seconds)
returntrue;
elsereturnfalse;
}
}
// What is returned if none of the if statements are entered?
}