1

This question is related to GCC says "syntax error before numeric constant" in generated header file from bison and I'm getting an error concerning enum (I think), but the answers there only gave the reason for why one might see the error, "error: syntax error before numeric constant." Unless I glossed over it, I didn't see any good solutions to avoid this problem (except of course to simply rename our enumeration constants). Thus, my question is: besides simply renaming enum constants to avoid this naming conflict, are there other (preferable) ways to get around this problem? Using namespaces does not seem to work.

UPDATE (for namespaces): I get this error:

enum.cpp:5:5: error: expected identifier before numeric constant
enum.cpp:5:5: error: expected ‘}’ before numeric constant
enum.cpp:5:5: error: expected unqualified-id before numeric constant
enum.cpp:7:1: error: expected declaration before ‘}’ token

from this program:

#include <sys/ioctl.h>

namespace mine {
  enum test {
    NCC
  };
}

int main(int argc, char** argv)
{ 
  return 0;
}

Note, I get the same error when compiling this program:

#define NCC 5

namespace mine {
  enum test {
    NCC
  };
}

int main(int argc, char** argv)
{ 
  return 0;
}
Community
  • 1
  • 1
t2k32316
  • 993
  • 1
  • 13
  • 29
  • 2
    Please show how namespaces were applied so we can diagnose it. – wallyk May 14 '14 at 00:22
  • 3
    If it's preprocessor symbols you're colliding with, namespaces won't help. – Fred Larson May 14 '14 at 00:23
  • updated now with how namespaces doesn't seem to work. – t2k32316 May 14 '14 at 00:29
  • This is why the preprocessor is regarded as poisonous in some circles. Avoid names that are reserved by the headers that you use. – Jonathan Leffler May 14 '14 at 00:57
  • This gets annoying when developing a large application that works with certain enums for a while and then suddenly you include another header (e.g. sys/ioctl.h) that breaks everything! It's easy to avoid the reserved names when starting out... but including headers several months later that break things is what causes trouble. – t2k32316 May 14 '14 at 01:33

2 Answers2

2

The only way I know of to do this is to undefine the contants/symbols you're about to redefine in your enumeration:

#include <sys/ioctl.h>
#undef NCC

namespace {
    enum {
        NCC
    }
}

This compiles.

Keep in mind that I'm assuming you really want to redefine that symbol. If so, that's how you do it.

alpartis
  • 1,086
  • 14
  • 29
0

In C++, you can use a namespace to keep them unconfused.

wallyk
  • 56,922
  • 16
  • 83
  • 148