0

Why does error appear when I output like the following example? *In my class CTimeSpan, I create like the following example: It's error cout<<tm1++;

#include <iostream>
using namespace std;
class CTimeSpan {
private:
    unsigned int day; //day
    unsigned int hour;
    unsigned int minute;
    unsigned int second;

public:
    ostream& operator<<(ostream& os,const CTimeSpan& tm)
    {
        os << tm.day << "d"
           << " " << tm.hour << "h"
           << " " << tm.minute << "p"
           << " " << tm.second << "s";
        return os;
    }
    CTimeSpan CTimeSpan::operator++(int s) {
            CTimeSpan temp = *this;
            ++* this;
            return temp;
     }
};
int main()
{
    CTimeSpan tm1{ 1, 2, 3, 4 };
    cout << tm1++; 
}

*thank you very much. Help me.

dophison
  • 1
  • 1
  • Your `<<` overload must take a `const CTimeSpan &` parameter, and not a `CTimeSpan &`. You also overloaded the `++` prefix operator instead of the postfix one. Your `++` overload should take a stub `int` parameter. – Sam Varshavchik May 30 '21 at 15:13
  • @SamVarshavchik sorry, my code is incorrect , I have already edited, but my problem hasn't been solved yet. I want to cout< – dophison May 30 '21 at 15:28
  • See "Unary arithmetic operators" in the linked question for an explanation of how to correctly implement the postfix `++` operator. – Sam Varshavchik May 30 '21 at 15:40

0 Answers0