5

I am trying to deal with operator overloading at the first time, and I wrote this code to overload ++ operator to increment class variables i and x by one.. It does the job but the compiler showed these warnings:

Warning 1 warning C4620: no postfix form of 'operator ++' found for type 'tclass', using prefix form c:\users\ahmed\desktop\cppq\cppq\cppq.cpp 25

Warning 2 warning C4620: no postfix form of 'operator ++' found for type 'tclass', using prefix form c:\users\ahmed\desktop\cppq\cppq\cppq.cpp 26

This is my code:

class tclass{ public: int i,x; tclass(int dd,int d){ i=dd; x=d; } tclass operator++(){ i++; x++; return *this; } }; int main() { tclass rr(3,3); rr++; rr++; cout<<rr.x<<" "<<rr.i<<endl; system("pause"); return 0; } 

3 Answers 3

12

This syntax:

tclass operator++() 

is for prefix ++ (which is actually normally written as tclass &operator++()). To distinguish the postfix increment, you add a not-used int argument:

tclass operator++(int) 

Also, note that the prefix increment better return tclass & because the result may be used after: (++rr).x.

Again, note that the postfix increment looks like this:

tclass operator++(int) { tclass temp = *this; ++*this; // calls prefix operator ++ // or alternatively ::operator++(); it ++*this weirds you out!! return temp; } 
Sign up to request clarification or add additional context in comments.

5 Comments

++*this; wierds me out. I use ::operator++();. Which wierds others out.
++*this; shows error saying: Error 1 error C2675: unary '++' : 'tclass' does not define this operator or a conversion to a type acceptable to the predefined operator c:\users\ahmed\desktop\cppq\cppq\cppq.cpp 14
@Shahbaz: I am using MS VC++.
Microsoft Visual C++ is one of the least standard-conforming compilers. (Specially version 6 which was written before C++ became standard). Personally, I would suggest coding in a real compiler. If you are stuck with it, you'd have to refer to msdn to understand its shortcomings and its workarounds. Either way, the way I wrote it is the standard.
@Shahbaz: MSVC is definitely a real compiler. MSVC6 was replaced in 2002. That's (roughly) the equivalent of GCC 3.0.4. More likely he did something wrong. Unrelated: the return type of operator++() is usually tclass&.
6

There are two ++ operators. You defined one and used the other.

tclass& operator++(); //prototype for ++r; tclass operator++(int); //prototype for r++; 

Comments

5

There are separate overloads for postincrement and preincrement. The postincrement version's signature is operator++(int), while the preincrement's signature is operator++().

You have defined operator++(), so you've only defined preincrement. However, you use postincrement on an instance of your class, so the compiler tells you that it will use a call to the preincrement function because no postincrement is defined.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.