CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Question 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?

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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

  3. #3
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: increment operator problem

    Quote 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

  5. #5
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: increment operator problem

    Quote 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

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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
  •  





Click Here to Expand Forum to Full Width

Featured