user1523271
user1523271

Reputation: 1063

Strange Error C2065: 'ERROR' : undeclared identifier

As part of a bigger project ( the ff activex plugin for Mozilla Firefox) there is this code snippet:

if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != ERROR)
{
        ::InvalidateRgn(m_hWndParent, hrgnClip, fErase);
}

When I build in VS2012 I get "Error C2065: 'ERROR' : undeclared identifier" ERROR is defined in wingdi.h like this:

...
/* Region Flags */
#define ERROR               0  // it wont build when this macro is used
#define NULLREGION          1  // it builds when this macro is used
#define SIMPLEREGION        2
#define COMPLEXREGION       3
#define RGN_ERROR ERROR
...

The strange thing is that if I replace ERROR (just to see if it builds OK) with NULLREGION or SIMPLEREGION (which are macros in the same file, just two lines below the offending one) in the if statement above, the code builds OK. When I use ERROR, the code wont build. Is it possible that the ERROR macro defined above gets masked by some keyword or another macro or some such by Visual Studio?

Upvotes: 2

Views: 2020

Answers (1)

Hans Passant
Hans Passant

Reputation: 942197

The problem here is that ERROR actually appears in the compile error message. That should not happen, the preprocessor should have substituted it with 0.

So somewhere in a .h file you #included after windows.h, some programmer took the 10 second shortcut to a macro name collision problem and wrote this:

  #undef ERROR

You'd need to find that line and remove it. That's likely to be difficult and liable to give you a maintenance headache since that's a file you don't own and might well be updated in the future, forcing you to make that change over and over again. The alternative is to redefine it yourself:

#define MYGDIERROR 0
...
if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != MYGDIERROR)
//etc...

Which still gives you a maintenance problem and requires you taking a bet that the return value definition of CombineRgn() is never going to change. That's a very safe bet, GDI is cast in stone.

Upvotes: 3

Related Questions