C++ – Post increment in operator overloading in c++

coperator-overloadingpost-increment

This is my post increment operator overloading declaration.

loc loc::operator++(int x)
{
    loc tmp=*this;
    longitude++;
    latitude++;
    retrun tmp;
} 

My class constructor

loc(int lg, int lt) 
{
   longitude = lg;
   latitude = lt;
}

In main function, I have coded like below

int main()
{
    loc ob1(10,5);
    ob1++;
}

While compiling this , i am getting the below error

opover.cpp:56:5: error: prototype for ‘loc loc::operator++(int)’ does
not match any in class ‘loc’
opover.cpp:49:5: error: candidate is: loc
loc::operator++() opover.cpp: In function ‘int main()’:
opover.cpp:69:4: error: no ‘operator++(int)’ declared for postfix ‘++’

Best Answer

Fix your class declaration from

class loc
{
    // ...
    loc operator++();
} 

to

class loc
{
    // ...
    loc operator++(int);
} 

[Edit removed misguided remarks about returning by value. Returning by value is of course the usual semantics for postfix operator++]

Related Topic