0

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 ‘++’

Alok Save
  • 202,538
  • 53
  • 430
  • 533
Heartly
  • 75
  • 1
  • 8

3 Answers3

4

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++]

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 4
    Why should we have parameter in `operator++` function? This is unary operation.. we don't have to pass anything inside – Vitaly Dyatlov Nov 04 '11 at 08:02
  • @VitalyDyatlov: I don't think we have to. You'd have to ask the OP what he wants to achieve. Regardless, the prototypes should match and I don't think the C++ specs forbid a parameter list. There just won't be a very useful way to invoke the operator except by calling `obj.operator++(42)`. Fixing my recommendation though, since you are right, this smells like a mistake – sehe Nov 04 '11 at 08:04
  • 3
    @VitalyDyatlov the parameter is to distinguish between post- and prefix increment. – AndersK Nov 04 '11 at 08:09
  • 1
    Sorry, I was wrong.. apologies. Here http://stackoverflow.com/questions/4421706/operator-overloading is explained overloading. without input parqams we have prefix overloading `++x`, with `int` as function params we have postfix overloading `x++`, so this is just to differentiate prefix and postfix.. sorry for making mistake – Vitaly Dyatlov Nov 04 '11 at 08:09
  • @Anders yes, just realized that – Vitaly Dyatlov Nov 04 '11 at 08:10
  • 1
    *Returning copies from `operator++` **is** the usual semantics* if you are implementing post-increment rather than preincrement. What you suggest is implementing a *different* operator that has different semantics – David Rodríguez - dribeas Nov 04 '11 at 08:15
  • @DavidRodríguez-dribeas and @Anders: oops. Yeah, I confused by a two-step thinko (starting with me adding `&` to the return type , which was my mistake) **Fixed** – sehe Nov 04 '11 at 09:28
3

you should have two versions of ++:

loc& loc::operator++() //prefix increment (++x)
{
    longitude++;
    latitude++;
    return *this;
} 

loc loc::operator++(int) //postfix increment (x++)
{
    loc tmp(longtitude, langtitude);
    operator++();
    return tmp;
}

And, of course, both functions should be defined in class prototype:

loc& operator++();
loc operator++(int);
Vitaly Dyatlov
  • 1,872
  • 14
  • 24
1

You didn't declare the overloaded operator in your class definition.

Your class should look something like this:

class loc{
public:
    loc(int, int);
    loc operator++(int);
    // whatever else
}

** edit **

After reading the comments, I noticed that in your error message it shows that you declared loc operator++(), so just fix that.

littleadv
  • 20,100
  • 2
  • 36
  • 50