-1
Line no:29: error: no 'operator++(int)' declared for postfix '++' [-fpermissive]

29 | idx2++; | ~~~~^~ Line no:30:9: error: no 'operator++(int)' declared for postfix '++' [-fpermissive] 30 | idx2++; | ~~~~^~ #include using namespace std; class index { private: int value;

public:
    index()
    {
        value = 0;
    }
    int getindex()
    {
        return value;
    }
    void operator++()
    {
        value = value + 1;
    }
};
main(void)
{
    index idx1, idx2;
    // Display index values
    cout << "\nIndex1= " << idx1.getindex();
    cout << "\nIndex2= " << idx2.getindex();
    // advance index objects with ++operator
    ++idx1;
    idx2++;//Getting error here
    idx2++;//Getting error here
    ++idx2;
    cout << "\nIndex1= " << idx1.getindex();
    cout << "\nIndex2= " << idx2.getindex();
}

Finding error in line 29 and 30

Rajiv
  • 1
  • 1

1 Answers1

0

It's because you haven't overloaded the post increment operator. You need to add:

auto operator++(int)
    {
        auto retval(*this); // copy *this
        ++value;
        return retval;      // and return the copy by value
    }

Note that it's also common to return a reference to *this from the operator you actually have overloaded.

auto& operator++()
    {
        ++value;
        return *this; // return a reference to *this
    }
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108