0

So, I'm still working on this big project to make it compiles on Linux. So, again, I got some error that shouldn't be allow to exist.

Here's some error that I got:

(1) error: expected identifier before numeric constant
(2) error: "Value" doesn't name a type

Here's a sample piece of code, simplified, that show you where I get those errors:

class Test
{
public:
  enum Value
  {
    V1 = 0,  // error (1) is here
    V2 = 1,
    V3 = 2
  };

private:
  Value value; // error (2) is here

public:
  // constructor and other function
};

Also, that piece of code is valid in some part of the project. But it's not in other parts. I did everything, I rename stuff to be sure it was not ambiguous, doesn't change anything.

Stuck with GCC 4.1.2

12
  • 4
    Check the preprocessor output. There's clearly a macro V! defined somewhere. May 24, 2012 at 14:15
  • 3
    Preprocess your source. Also lose the attitude
    – sehe
    May 24, 2012 at 14:15
  • 3
    I heard people telling me that their compiler is stupid many times already. I have to say that in the end it was never the compiler to blame.
    – ypnos
    May 24, 2012 at 14:16
  • 2
    @ypnos: As long as you don't use lots of lambdas in VS2010.
    – Puppy
    May 24, 2012 at 14:16
  • 4
    @widgg - we don't want to see more code from your project. We want you to reduce a copy of your project to the smallest possible file that demonstrates the problem. I suspect that, during the reduction process, you will find the problem yourself. See sscce.org and Machete Debugging.
    – Robᵩ
    May 24, 2012 at 14:19

1 Answer 1

7

The fragment compiles fine with g++ 4.4.3, and I expect it also compiles fine with g++ 4.1.2.

What you've encountered is the reason using #define for constants is a fundamentally evil thing to do in C++. One of the header files you've included incorporates a line like:

#define V1 42

The fastest solution is

#undef V1

although, if someone has been malicious enough to #define V1 then I sense similar defines for V2 and V3 in your future. In my personal experience windows.h and many of the X11 headers are widely responsible for introducing preprocessor definitions like these.

I'd like to offer advice on tracking the offending header down, but I usually resort to grep and/or seeing which headers make the error go away when removed.

When we encounter this problem our general practice is to

  • Minimise the number of places the offending header is included - and never include it in a header file.
  • Create a "safe" wrapper header that #undefs all the offending constants and replaces them with something more reasonable if necessary.
1
  • it solves this problem... there's more left, but it's almost done!
    – widgg
    May 24, 2012 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.