Program help

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;
}

bool Time::operator ==(Time otherTime) const
{
if ((hours==otherTime.hours) && (minutes==otherTime.minutes) && (seconds==otherTime.seconds))
return true;
else
return false;
}

bool Time::operator <(Time otherTime) const
{
if (hours<otherTime.hours)
return true;
if (hours > otherTime.hours)
return false;
if (hours==otherTime.hours)
{
if (minutes < otherTime.minutes)
return true;
if (minutes > otherTime.minutes)
return false;
if (minutes== otherTime.minutes)
{
if (seconds< otherTime.seconds)
return true;
else
return false;

}
}
}

bool Time::operator >(Time otherTime) const
{
if (*this<otherTime)
return false;
else
return true;
}
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
return false;
else
return true;
}
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:

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
bool Time::operator <(Time otherTime) const
{
    if (hours<otherTime.hours)
        return true;

    if (hours > otherTime.hours)
        return false;

    if (hours==otherTime.hours)
    {
        if (minutes < otherTime.minutes)
        return true;

        if (minutes > otherTime.minutes)
        return false;

        if (minutes== otherTime.minutes)
        {
            if (seconds< otherTime.seconds)
                return true;
            else
                return false;
        }
    }

    // What is returned if none of the if statements are entered?
}


Hopefully that clears it up.
Topic archived. No new replies allowed.