8

I added a file in source control which had an enum definition as:

enum { OK = 0, ERROR };

But on compilation it was throwing errors like "expected identifier before numeric constant." Did my research on that and the culprit was supposed to be 'OK' which was defined somewhere else in the code. So, i changed OK with say, OK_1, and the issue was, indeed, resolved.

However, I have not been able to find where in my code base was this 'OK' defined before. I ran a grep from top level and couldn't find it. I am pretty sure I have covered all the application related code, but OK wasn't there.

I think it's unlikely that it was a part of some shared library as compilation process didn't even reach linking phase. It could have come from one of the header files maybe.

Is there a way/linux tool that somehow can be tricked to find where this OK is coming from?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sahil
  • 359
  • 2
  • 5
  • 13

2 Answers2

4

If you are using C++ 11 take a look at enum class: http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

One big draw back of enum is that you cant have 2 enums with the same name. With enum class this draw back is gone, you can write thing like this:

enum class Color {RED, GREEN, BLUE}; 
enum class Feelings {EXCITED, MOODY, BLUE};

And later on in the code:

Color color = Color::GREEN;
if ( Color::RED == color )
{
    // the color is red
}

Code example is pasted from linked www page

3

Converting my comment to answer.

Looks like you need pre-processor output Can gcc output C code after preprocessing?

Community
  • 1
  • 1
Sameer Naik
  • 1,326
  • 1
  • 13
  • 28