So I have this enum in a header file:

enum class ServoPort {PORT1 = 1, PORT2, PORT3, PORT4, PORT5, PORT6};

The trouble is that when I try to use it in the primary file (by doing MyClass::ServoPort::PORT1) I get a compile error:

exit status 1
expected unqualified-id before numeric constant

However, if I change the enum to be like so:

enum class ServoPort {PORT8 = 1, PORT9, PORT10, PORT11, PORT12, PORT13};

Then I do not get a compile error. What the heck? Can someone please shine some light on why this is happening?

  • Board: UNO R3
  • IDE: v1.8.5
  • Please add which Arduino board and IDE version. Feb 12, 2019 at 20:54
  • 1
    @MikaelPatel done Feb 12, 2019 at 20:55
  • 1
    @MikaelPatel hmm, ok yeah I could see that being a problem if I was trying to do a #define PORT2 or something, but in this case I'm creating an enum that has a a member of that name. It seems to me that shouldn't cause a conflict? I'm from a Java background so maybe I don't know what I'm talking about... Feb 12, 2019 at 21:00
  • 1
    The defines are pre-processed symbols before compiling. Your definition gives an illegal syntax when replaced. PORT1 = 1, PORT2, becomes, 1 = 1, 2. Feb 12, 2019 at 21:03
  • 1
    @MikaelPatel I see, but shouldn't the preprocessor be smart enough to not do that? Feb 12, 2019 at 21:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.