Aurum
Aurum

Reputation: 11

C++ Operator Overloading Within an Already Overloaded Operator

I'm having a bit of an issue using overloaded operators in an already overloaded operator. In my following code, I have overloaded the && operator to compare two Course objects. The operator in turn goes to a function which calls other overloaded operators to compare private object variables of that object to compare them mainly:

Code:

bool operator&&(const DaysOfWeek& a, const DaysOfWeek& b);
bool operator&&(const TimeInterval& a, const TimeInterval& b);

Now, for my question. I have been using many overloaded operators in this project, but this is the first time I have had to call overloaded operators inside other overloaded operators. Unfortunately, my overloaded operators in the code above is not being called from my isOverlap function. So my question is: why is this and how can I correct it?

Any help would be GREATLY appreciated, as I am banging my head against the wall trying to get this to work. i have included the relevent code from Course.h and the functions and overloaded operator in Course.cpp. I have bolded the appropriate lines of code that I am having irregular output for (not using my overloaded operator).

Code:

bool Course::isOverlap(const Course& b) const
{
    DaysOfWeek tempDays = b.getDays();
    TimeInterval tempTime = b.getTime();
    if(this->instructor==b.getInstructor() &&
        &this->days&&(&tempDays) &&
        &this->time&&(&tempTime))
    {
        return true;
    }
    else
        return false;
}

bool operator&&(const Course& a, const Course& b)
{
    if (a.isOverlap(b))
        return true;
    else
        return false;
}

Code:

#ifndef COURSE_H
#define COURSE_H

#include <string>
#include "TimeInterval.h"
#include "DaysOfWeek.h"

using namespace std;

class Course
{
    public:
        Course();
        Course(const string courseCode, const string section,
            const DaysOfWeek& days, const TimeInterval& time,
            const string instructor);
        void setCourse(string courseCode, string section,
            DaysOfWeek& days, TimeInterval& time, string instructor);
        string getCourse() const;
        string getSection() const;
        DaysOfWeek getDays() const;
        TimeInterval getTime() const;
        string getInstructor() const;
        bool isOverlap(const Course& b) const;
        bool isMatch(const Course& b) const;

    private:
        string courseCode;
        string section;
        DaysOfWeek days;
        TimeInterval time;
        string instructor;
};

bool operator&&(const Course& a, const Course& b);
bool operator==(const Course& a, const Course& b);

#endif //COURSE_H

I have also tried replacing the code I have with:

bool Course::isOverlap(const Course& b) const
{
    DaysOfWeek tempDays = b.getDays();
    TimeInterval tempTime = b.getTime();
    if(instructor==b.getInstructor() &&
        days && tempDays &&
        time && tempTime)
    {
        return true;
    }
    else
        return false;
}

As a friend had suggested, but this doesn't even compile (doesn't match the arguments to the overloaded && operator).

Upvotes: 1

Views: 812

Answers (2)

Daniel T.
Daniel T.

Reputation: 33967

   DaysOfWeek tempDays = b.getDays();
    TimeInterval tempTime = b.getTime();
    if(this->instructor==b.getInstructor() &&
        &this->days&&(&tempDays) &&
        &this->time&&(&tempTime))

In the above you are using logical and for the address of days compared to the address of tempDays, you should be comparing the objects, not the addresses. Same with time and tempTime.

Upvotes: 1

James McNellis
James McNellis

Reputation: 355069

This:

instructor==b.getInstructor() && days && tempDays && time && tempTime

is equivalent to this:

(((((instructor==b.getInstructor()) && days) && tempDays) && time) && tempTime)

First instructor==b.getInstructor() is evaluated, yielding a bool. Then the compiler sees && days and tries to find an overload of && that takes a bool and a DaysOfWeek. There isn't one, so it yields an error.

To use your overloaded && alongside the built-in &&, you need some parentheses to force grouping of the subexpressions:

instructor==b.getInstructor() && (days && tempDays) && (time && tempTime)
                                 ^                ^    ^                ^

That said, I would strongly advise going back to your instructor and telling him that he is crazy. Overloading the && operator (or the || operator) is almost always wrong because it breaks the normal semantics of the operator (when overloaded, those two operators cease to short-circuit).

Upvotes: 3

Related Questions