0

I made a operator overloading function in a class. And I made another function which recalls the overloading function in the class.

I want to use the function which recalls the operation overloading function in main function so I wrote like this in the main function :

#include <iostream> ... class Zealot { int x; .... void operator++() { Zealot s; s.x = -50; for (auto i = 0; i < 2; ++i, tail.push_back(s)); } void Collision() { ... (*this)++; // Error : C2676 ... } ... }; Zealot z; int main() { z.Coliision(); } 

I got Error C2676 so I couldn't compile the source.

What should I do to work it well? I need your big helps.

2
  • 3
    The operator you've defined is the prefix form of ++, so you'd invoke it like ++(*this);. The postfix form is distinguished by a parameter (that's otherwise unused) of type int, so it's: void operator++(int). Note that operator++() should normally return a reference to the just-incremented object. Commented May 13, 2017 at 5:28
  • @Jerry Coffin Thank you for your help! I will keep that in mind. Commented May 13, 2017 at 5:39

1 Answer 1

1

What you have overloaded is the pre-increment operator.
What you are using is the post-increment operator.

You can either use the pre-increment operator:

++(*this); 

or implement a post-increment oprator:

void operator++(int) { ... } 

To be idiomatic, you should change the return values of those functions though.

Zealot& operator++() { ... } Zealot operator++(int) { ... } 

You can read more on operator overloading at http://en.cppreference.com/w/cpp/language/operators.

Sign up to request clarification or add additional context in comments.

Comments