C++11 Compilation Error for enumerated type declaration as expected } before numeric constant

619 Views Asked by At

I have the following source file (test.c):

#include <iostream>
enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
int main()
{
  return 0;
}

When I compile without -std=c++11 it compiles fine.

g++  test.c -o test

When compiled with -std=c++11, it comes with compilation error: g++ -std=c++11 test.c -o test

test.c:3:16: error: expected identifier before numeric constant
  enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
                ^
test.c:3:16: error: expected â}â before numeric constant
test.c:3:16: error: expected unqualified-id before numeric constant
test.c:3:42: error: expected declaration before â}â token
  enum ecodes { ENOKEY = -1, EDUPKEY = -2 };

Compiler used is GNU g++ 4.9.2 on Linux.

bash-4.2$ g++ --version
g++ (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6)

Please help.

1

There are 1 best solutions below

0
Paul R On

ENOKEY is an error code defined in <errno.h>:

#define ENOKEY      126 /* Required key not available */

Presumably <errno.h> is being #included by <iostream> on your build platform (at least when -std=c++11 is specified), so the line:

enum ecodes { ENOKEY = -1, EDUPKEY = -2 };

gets preprocessed to:

enum ecodes { 126 = -1, EDUPKEY = -2 };

Hence the error.

Note: your original example code had INVALID in place of ENOKEY, so no one else was able to reproduce the problem.

Take home message: when asking questions always provide a proper MCVE with actual code that reproduces the error, not an approximation of where you think the problems lies.