0

i'm getting the following error for my code: type 'Date' does not provide a call operator whenever i try to do the following code:

date1++(25)

This is the postfix operator code here. What's wrong with it?

Date Date::operator++(int)
{
  Date days(month, day, year);
  days.increment();
  return days;
}

increment function:

void Date::decrement()
{
    //check if the day is the 1st of the month
    if (day == 1)
    {
        day = 30;
        //check the month and yar. if it's the first month then we decrease the year by 1 and change month to 12
        if (month == 1)
        {
            month = 12;
            year = year - 1;
            return;
        }

        // if the month is not 1, then we decrease month by 1
        month = month - 1;
        return;
    }
    else {
        //otherwise we just decrease day by 1
        day = day - 1;
        return;
    }
}
  • [`operator++(int)`](https://en.cppreference.com/w/cpp/language/operator_incdec) isn't a binary operator, despite what the syntax may suggest. The dummy `int` parameter is just used to distinguish between the pre and post versions. `date1++(25)` is equivalent to `(date1++)(25)`, which naturally results in an error if `Date::operator(int)` isn't defined. – Brian61354270 Nov 03 '21 at 03:25
  • Additionally, your operator _should_ modify the actual value. Right now it is just creating a temporary, incrementing the temporary and returning it while leaving the original object unchanged. So calling `date1++` actually does nothing. – paddy Nov 03 '21 at 03:32
  • The "call operator" from the error message would be `SomeType Date::operator()(int)`. The lack of such an operator is what your compiler complained about. (The expression `date1++` evaluates to a `Date` object, so a pair of parentheses after that says to apply `operator()` to that `Date` object.) As far as your compiler is concerned, your postfix operator code is fine. – JaMiT Nov 03 '21 at 03:39
  • so i would have to do date1.operator++(25) for it to increment 25 times? (in this case 25 days) not sure if i do follow – ASM student Nov 03 '21 at 04:17
  • @ASMstudent No, to increment `date1` 25 times in one expression using the postfix increment operator, you would need `date1++++++++++++++++++++++++++++++++++++++++++++++++++` (apply the operator 25 times). The `int` parameter to postfix increment is an unusable dummy parameter (as should have been explained when you learned about defining your own postfix increment operator). – JaMiT Nov 03 '21 at 21:27
  • ah, ok that makes a lot of sense. thank you very much @JaMiT! – ASM student Nov 04 '21 at 07:52

0 Answers0