0

I'm using this code in my previous question: Adding the year implementation in c++ using a class

I want to use an if statement to check for dates in a way that if the day was 31 it gets back to 0 and the month gets incremented by one. I even tried to write another method and use it inside of the + operation but this failed as well because I'm incrementing the day in the return function inside of the operation declaration. As a result, it will need to be incremented before checking for the conditions first ! but what if the number was initially 31? there is no month that has 32 days !

I tried to use it but because of my implementation it didn't work as it should

My other question is that I'm trying to use a Boolean reference check with the operation == as well

This is what I've done so far :

bool operator==(const Date&) const;

bool Date::operator==(const Date& date) const
{
    if (day == date.day && monthnum == date.monthnum && year == date.year)
        return true;
    else return false;

}

but for some reason when I try to test it in the main by saying for example, date1==date2, it doesn't compile ! am I writing it wrong ?

"no operation == matches these operands" this is the error I get when I try to compile the code

Community
  • 1
  • 1
rullzing
  • 622
  • 2
  • 10
  • 22

1 Answers1

2

I want to use an if statement to check for dates in a way that if the day was 31 it gets back to 0 and the month gets incremented by one.

This is as simple to implement as:

if (day == 31) {
    day = 0;
    monthnum++;
}

I try to test it in the main by saying for example, date1==date2, it doesn't compile ! am I writing it wrong ?

Yeah well, you are declaring a free function operator==, while what you want is a member function. Inside Date do:

class Date {
public:
    // ...
    bool operator==(const Date&) const;
    // ...
};

You can also use a free function, to be honest, but that would require more changes and it generally is the same. Just in case you want to use it here's how:

bool operator==(const Date& lhs, const Date& rhs) {
    return (lhs.day == rhs.day && lhs.monthnum == rhs.monthnum && lhs.year == rhs.year);
}

(I've removed the redundant if-else pair).


The compiler states that "no operation == matches these operands". I simply have this code in my main: cout << date1 == date2;

Yes, you should do this instead:

cout << (date1 == date2);

otherwise what the compiler reads is this:

(cout << date1) == date2;
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I think the function that you considered to be free is inside the class, since this is the implementation: `bool Date::operator==(const Date& date) const` – Alexandru Barbarosie Feb 02 '14 at 22:45
  • @AlexandruBarbarosie, I very much doubt so, considering that he is triggering a compilation error. – Shoe Feb 02 '14 at 22:47
  • No one knows what he does in main. If the function would have been free, `Date::operator==` would also trigger an error. – Alexandru Barbarosie Feb 02 '14 at 22:49
  • @AlexandruBarbarosie, I guess we will see when the OP replies. I'll happily delete this answer if I guessed wrong. – Shoe Feb 02 '14 at 22:50
  • I am using the first code in my program and it is not compiling. I've tried the second one and I still have the same problem. The compiler states that "no operation == matches these operands". I simply have this code in my main: cout << date1 == date2; – rullzing Feb 02 '14 at 23:07
  • THANK YOU ! I didn't expect that coming. – rullzing Feb 03 '14 at 00:53