|
-
February 3rd, 2005, 10:25 PM
#1
increment operator problem
The following code does not compile, why?
1 int main ()
2 {
3 enum Day { Sun, Mon, Tue };
4
5 Day& operator++ (Day& d)
6 {
7 d = (Day) (d + 1);
8 return d;
9 }
10 }
Line 5 generates a compiler error similar to the following...
Unrecognized type name "Day"
This was compiled on a Sun box running Solaris 8 with their C++ compiler version 5.3.
Is there some fundamental problem with this code, or is this possibly a bug in the compiler?
-
February 3rd, 2005, 11:00 PM
#2
Re: increment operator problem
The problem is that you are attempting to overload and operator within a function. Operators can only be overloaded for class types, and the overloaded operator's declaration goes into the class declaration. That's not to mention that functions can't be defined inside other functions period...
Old Unix programmers never die, they just mv to /dev/null
-
February 3rd, 2005, 11:44 PM
#3
Re: increment operator problem
I do not believe that is true. Read Bjarne Stroustrup's "The C++ Programming Language". There is a section that speaks of operating overloading for defined data types outside of a class. For definitions outside a class there is an additional argument that must be specified (as in this example). In fact, an example (using an enumeration) appears in his book, and is very similar to this example.
-
February 3rd, 2005, 11:49 PM
#4
Re: increment operator problem
 Originally Posted by sszd
I do not believe that is true.
Nested functions are not allowed in C++, and you have a nested function.
Regards,
Paul McKenzie
-
February 3rd, 2005, 11:53 PM
#5
Re: increment operator problem
Ok, I'll buy that. Do you think it will compile if I move the enumeration and the operator++ function definition outside of main? Thanks for your help.
-
February 4th, 2005, 12:00 AM
#6
Re: increment operator problem
 Originally Posted by sszd
Ok, I'll buy that. Do you think it will compile if I move the enumeration and the operator++ function definition outside of main?
Yes.
Regards,
Paul McKenzie
-
February 4th, 2005, 04:57 AM
#7
Re: increment operator problem
Note that overloading operator++(Day&) only gives you the prefix form of ++. For completeness, you should also implement the postfix form by overloading operator++(Day&, int). The second argument is not used - it's only there to distinguish the two forms.
And don't forget operator-- if appropriate.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|