Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict between a namespace and a define

I have this serious problem. I have an enumeration within 2 namespaces like this:

namespace FANLib {
namespace ERROR {

    enum TYPE {

        /// FSL error codes
        FSL_PARSER_FILE_IERROR,...

and somewhere else in my code, I use it like this:

FANLib::Log::internalLog(FSLParser::FILE_IERROR, file_ierror, true, FANLib::ERROR::FSL_PARSER_FILE_IERROR);

All compiles fine and well, but if I happen to include "windows.h", I get errors! The problem is in "WinGDI.h" which has this line:

#define ERROR               0

and makes the compiler think that, after FANLib::..., there is a zero! The error I get is :

Error 1 error C2589: 'constant' : illegal token on right side of '::'

Error 2 error C2059: syntax error : '::'

Error 3 error C2039: 'FSL_PARSER_FILE_IERROR' : is not a member of '`global namespace''

Is there anything I can do about this, without having to change my namespaces due to some thoughtless #define? I have read in another post that I could #undef ERROR, but how safe is that?

like image 463
Bill Kotsias Avatar asked Sep 04 '09 08:09

Bill Kotsias


3 Answers

Generally, you should avoid using all-caps identifiers as they are used for macros. In this case, I'd rename the namespace.

(As a side note, <windows.h> #defines other stuff like GetPrinter and indeed it gets annoying. I usually go with #undef then. It also helps to only include <windows.h> in .cpp files and make sure the scope affected by the header is as small as possible.)

like image 68
avakar Avatar answered Sep 21 '22 21:09

avakar


Renaming your namespace is the cleanest, safest, most interoperable solution.

like image 33
Michael Foukarakis Avatar answered Sep 23 '22 21:09

Michael Foukarakis


Hopping on the "rename your namespace" bandwagon, simply because ERROR is way too common and vague a name. Find something more descriptive.

like image 2
suszterpatt Avatar answered Sep 21 '22 21:09

suszterpatt