Thread: Operator++ with enum class

  1. #1
    Registered User
    Join Date
    Dec 2011

    Operator++ with enum class

    I have this in my header file:
            enum class Azimuth {NORD = 0,
                NE = 45,
                EST = 90,
                SE = 135,
                SUD = 180,
                SO = 225,
                OUEST = 270,
                NO = 310,
            Azimuth& operator++();
    And this in my implementation file:
    Bousole::Azimuth& Bousole::Azimuth::operator++ ()
        //*this = (*this==Azimuth::NO) ? Azimuth::NORD : *this+NE;
        return *this;
    Upon compiling, it complains that Bousole::Azimuth is not a class or namespace

    g++ -Wall -g -std=c++0x -I/usr/includes/c++/4.7.1 -c main.cpp -o main.o
    g++ -Wall -g -std=c++0x -I/usr/includes/c++/4.7.1 -c Bousole.cpp -o Bousole.o
    Bousole.cpp:5:49: error: ‘enum class Bousole::Azimuth’ is not a class or a namespace
    Bousole.cpp:5:49: error: ‘Bousole::Azimuth& operator++()’ must have an argument of class or enumerated type
    make: *** [Bousole.o] Error 1

  2. #2
    Registered User
    Join Date
    Jun 2005
    enums cannot have members, member functions, nor a this pointer. enum class does not change that.

    Overloaded forms of operator++() for an enum (whether a vanilla enum or a C++11 enum class) would be defined like this.
    // prefix form
    OurEnum& operator++ (OurEnum &x)
         //   increment value of x here
        return x;
    // postfix form
    OurEnum operator++ (OurEnum &x, int)
         Bousole::Azimuth temp(x);
         //   increment value of x here
        return temp;
    I must admit, in your case, I consider incrementing an enum with values 45 apart is not sensible. Incrementing does not mean "add 45".
    Last edited by grumpy; 04-29-2013 at 04:17 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Quote Originally Posted by grumpy View Post
    Incrementing does not mean "add 45".
    in the context of cardinal compass directions, and especially in the context of an enum representing those directions, it could. incrementing a pointer points it to the next object, and not necessarily the next byte in memory, so increment doesn't always mean +1 in every context of an operation. +1 to a cardinal direction logically implies +22.5, +45, or +90 degrees, depending on how fine you want the directions to be.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Yes indeed, that is the idea. As the dial is rotated clockwise, degrees increase by 45 and at NW position, reset to North. But for some reason I cannot make this work.
    I think I am going to revert to good old switch for now.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    NO = 310

    How would it work if your granularity is 45 degrees?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access enum class and its values outside of the class.
    By mariostg in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2013, 08:51 PM
  2. nested enum operator overloading
    By BdON003 in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2009, 05:41 PM
  3. custom enum class (operator overloading)
    By l2u in forum C++ Programming
    Replies: 16
    Last Post: 04-28-2008, 03:22 PM
  4. enum and overloading operator<<
    By letdoit in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2004, 06:13 PM
  5. enum in class
    By gipsy in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2002, 07:16 AM