views:

332

answers:

2

I have a very simple macro that I use for shorthand when declaring exceptions. When in debug mode it adds the current file and line number.

I'm in the process of modifying my code to support unicode, and suddenly I'm getting "undeclared identifier" errors whenever my macro is used. I'm probably missing something really simple, as the macro itself is rather simple. Can anyone tell what the issue is?

Here's the macro declaration:

#ifdef _DEBUG
#define EXCEPTION(msg, mm) Exception(msg, mm, _T(__FILE__), _T(__LINE__))
#else
#define EXCEPTION(msg, mm) Exception(msg, mm)
#endif

I don't think it's needed, but just in case, here's the Exception constructor declaration:

Exception(LPCTSTR msg, BOOL manageMsg = FALSE, LPCTSTR f = NULL, int l = -1);

When compiling in release mode I don't get any errors, but when in debug mode I do, so it's something with the __FILE__ and __LINE__ bits, but I can't figure out what the actual issue is.

+3  A: 

The __LINE__ macro evaluates to an integer. The _T macro puts an L on the front of strings to make them Unicode strings. It's meant to be followed by an opening double quotation mark, like L"file.cpp". But in your case, it's followed by the integer literal that __LINE__ expands to. You're getting something like this: L23. Get rid of the second _T call.

#define EXCEPTION(msg, mm) Exception(msg, mm, _T(__FILE__), __LINE__)

This might have been more easily diagnosed if you had included the name of the identifier that the compiler didn't recognize. Compilers usually include that information in their error messages.

Rob Kennedy
There was no name. The entire error is "undeclared identifier". That's why I couldn't figure it out.
Herms
That was it. I was a bit overzealous with my additions of the _T() macro. I knew it was something dumb!
Herms
A: 

This wasn't exactly the same problem I had, but I am posting the solution to my issue here because I came across this question during my investigations.

If you encounter this error message with a multiline macro, turn on visible whitespace in your editor. You may have whitespace after the '\' continuation character at the end of the line:

#define FOO_BAR(aFoo) \ 
    FOO_BASE(aFoo, "bar")

The space at the end causes the parser to parse the first line of the macro definition as complete (FOO_BAR expands to '\ '), and the second line is interpreted as a function declaration, hence "undeclared identifier 'aFoo'").

Tim Keating