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); 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 Code:
#ifndef COURSE_H |
Advice: read C++ book and get more practice.
The problem is with this part: Quote:
For a start, try replacing this: Code:
bool Course::isOverlap(const Course& b) const Code:
bool Course::isOverlap(const Course& b) const |
Quote:
I tried doing your replacement and it gave me the compiler error saying that my arguments do not match what I defined for my overloaded operator. If you have any other ideas, please let me know. They are greatly appreciated. Quote:
You were also correct about the "&&" addressing twice as well. I will try to avoid this in the future. Unfortunately the assignment forces me to use the && operator to compare two Course objects. |
Quote:
Code:
class Course |
Quote:
As long as the operator takes two objects, it works fine. In fact, like I said, I used && to get to where I am stuck currently from comparing two Courses. I used the following code to get to the isOverlap function in the overloaded && operator: Quote:
|
Had someone else find an answer to this, you must use parens around every single (variable&&variable) statement, as it will break with the other && operator in an if-statement.
|
Quote:
Code:
if (scheduler[i]&&scheduler[j] && days != "O") Don't get carried away with defining overloaded operators merely for the sake of it; it could lead to poor coding. |
Quote:
I don't think you were told to write an operator&&() that compares two objects. I think you were told to write an operator==() that compares two objects, and inside that operator==() you should use the ordinary (int or bool) && to combine the results of comparing members. Before you turn in a mess due to that misunderstanding, do you have an opportunity to ask the instructor for clarification? Quote:
Assume for the moment, you really do want to define a function that acts like operator==() but is named operator&&(), then you want to use it in an expression like: x1 && x2 && y1 && y2 where the && should be the weird operator&&() you defined, while the && should be the ordinary && combining the bool results. The fact that you defined operator&&() doesn't change the operator precedence of &&, so writing x1 && x2 && y1 && y2 has the same meaning as ( (x1 && x2) && y1 ) && y2 which is not what you want. So you need parens around (y1 && y2) to correct the order of evaluation. Parens around (x1 && x2) aren't needed, but certainly help to make the weird intent of the code a little clearer. |
Quote:
Quote:
Code:
bool Course::isOverlap(const Course& b) const |
All times are GMT -5. The time now is 05:24 PM. |