Strange Error C2065: 'ERROR' : undeclared identifier - c++

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?

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.

Related

C++ function inside of a macro missing ;

I'm having trouble with writing an assignment for uni in an old Borland c++ standard.So I have this macro which HAS to be used like this: PREPAREENTRY(9,0); where the first argument will always range from 0 to 255 and second will always be 0 or 1. Without going into much detail unless necessary it's supposed to automatically generate a unique interrupt function for every entry in the interrupt vector table.
My code is as follows:
#define PREPAREENTRY(ivtNo, oldInterrupt) \
extern IVTEntry ivtEntry##ivtNo; \
void interrupt interrupt##ivtNo(...){ \
ivtEntry##ivtNo.signal(); \
if(oldInterrupt) \
ivtEntry##ivtNo.oldInterrupt(); \
} \
IVTEntry ivtEntry##ivtNo(ivtNo, interrupt##ivtNo);
(I've tried not putting ; on the last line of macro definition, but it still gives me the same error)
When I don't use the macro and switch every ivtNo with 9 and oldInterrupt with 0, the code works. But when I use the macro the code can't compile and it gives me this error Error ..\src\keyevent.cpp 17: Statement missing ; in function interrupt interrupt9(...). I've tried moving around a bunch of stuff and writing it several times but it's always either that error or missing ) error.
I will provide you with more details if needed, but if anyone has any clue what could be causing these errors I'd be very grateful!
SOLVED
Macro argument and function inside of IVTEntry class that I'm using inside of the macro had the same name. So the macro called ivtEntry9.0(); instead of ivtEntry9.oldInterrupt();

Can you help me fix error about Bluetooth Low Energy (C++)?

I use VS2013 on Win 8.1.
I have a solution with some projects. One of them is a project contain code connect Bluetooth Low Energy. I build it success.
Another project what reference to above project. But when I build, show error "error C2061: syntax error: identifier 'BLUETOOTH_ADDRESS ' ".
I find out why have this error. In "BluetoothApis.h" , condition "#if (NTDDI_VERSION >= NTDDI_WINXPSP2)" don't happen. Detail :
#if (NTDDI_VERSION >= NTDDI_WINXPSP2)
typedef ULONGLONG BTH_ADDR;
typedef struct _BLUETOOTH_ADDRESS {
union {
BTH_ADDR ullLong; // easier to compare again BLUETOOTH_NULL_ADDRESS
BYTE rgBytes[ 6 ]; // easier to format when broken out
};} BLUETOOTH_ADDRESS_STRUCT;
#define BLUETOOTH_ADDRESS BLUETOOTH_ADDRESS_STRUCT
Help me!
In your Visual project(s), you have to #define (in compiler's preprocessor options) which version of the SDK you're using (or want to use). So, typically, you would add compiler's preprocessor terms like :
NTDDI_VERSION=NTDDI_WIN7
_WIN32_WINNT=WIN32_WINNT_WIN7 (if Win7 is your target)
Use appropriate constants for Win8...

C++ Weird error. Fails to compile

I have been using the allegro 5 libraries for developing a game in C++ for some time. Today I got some weird error:
I have a class called level. I have a header file called levelhandler.
Here's how it looks:
#pragma once
#include "level.h"
level level_1;
level *currentlevel;
void initialize_levels()
{
currentlevel = &level_1;
}
When I try to compile it gives me strange errors like:
error C2086: 'int level' redefinition
error C2143: syntax error : missing ; before 'level_1'
I remember that it could compile before, and I did use currentlevel->Player.X a lot of times, but now I have a lot of that and it gives errors like these:
error C2227: left of '->Player' must point to a class/struct/generic type
error C4430: missing type specifier - int assumed
header pasted from comment
#pragma once
#include "entity.h"
// some more includes
class level {
public:
enum Tileset { ... };
enum Tile { ... };
int tiles[200][200];
player Player;
level(void);
~level(void);
};
Such errors are hard to find as long as you look at the "Error List" pane. Select View/Output to show the "Output" view. The line after the error C2086 shows the original definition of level.
You fill find an
int level;
there as the C2086 tells you. If it's the line
level level_1;
of your fist example you will have to check the last header file include in your compilation unit. It might end with an int or it has a unbalanced #if clause.
To find the exact location start using a Short, Self Contained, Correct (Compilable), Example. This helps you to find the bug and saves time of other with their crystal balls.
Edit:
Another way to find the reason for this unexpected behavior is to see the preprocessor output. Set the Generate Preprocessed File option int the C/C++/Preprocessor project property page to With line numbers (/P) and look in the generated <sourcefile>.i
Check that that level.h file has included what you intended.
Ok, so I've been tinkering around with my project the weekend, and I finally found out what whas the problem that gave me so many weird compiler errors. It seems that I had a lot of cases where two header files were including each other, and the compiler really didn't like that, so I corrected that, and now I'm ok. Thank you all for helping me, and have a great day!

How do I use _DEBUG_ERROR in my own code?

Inside the <vector> header, there is code like this:
_DEBUG_ERROR("vector iterator not dereferencable");
_SCL_SECURE_OUT_OF_RANGE;
Which halts the program with a message and gives the programmer a chance to debug the code.
For a little toy project, I want to use _DEBUG_ERROR myself. It is defined in <xutility>.
However, when I include <xutility> and try to use _DEBUG_ERROR, I get the following error:
error C3861: "_Debug_message": identifier not found.
But _Debug_message is defined inside <xutility>, in line 28! Why does the compiler complain?
Also, is there another (maybe even somewhat portable?) way to halt the program for debugging?
Not 100% certain but I'm fairly sure it's actually std::_Debug_message. And PlasmaHH is right: assert() is the normal solution. You can write assert(!"message") to get a custom message. (Note the !)
You can use ASSERT or _ASSERT macro for assert-and-debug. Or, you can craft your own assert-macro. Use the definition of _ASSERT (taken from crtdbg.h):
#define _ASSERT_EXPR(expr, msg) \
(void) ((!!(expr)) || \
(1 != _CrtDbgReportW(_CRT_ASSERT, _CRT_WIDE(__FILE__), __LINE__, NULL, L"%s", msg)) || \
(_CrtDbgBreak(), 0))
The important function here is _CrtDbgReportW, which will display the assertion dialog-box having three standard buttons (Abort, Retry and Ignore). Depending on return value you can then call other functions. In the macro given above, function _CrtDbgBreak is called when user hits 'Retry' button (which causes function to return 1, see MSDN).
You may write to a log file, display to debug output window (using OutputDebugString), or do things you may like.

Visual Studio not recognizing certain classes

I keep getting the error:
error C2146: syntax error : missing ';' before identifier 'mCameraFrame'
for the line of code:
Frame mCameraFrame;
So clearly my frame class isn't being found somehow. I have the frame.h header file (which defines the Frame class) directly included in this file. Why doesn't visual studio recognize it?
The error is coming from previous lines of code, possibly in a header file.
For example:
struct foo
{
int a;
}
Frame mCameraFrame;
Notice the missing ; after the }? That makes the Frame legal as an instance of the structure, but now there's a missing ; before mCameraFrame, resulting in the kind of error you reported.
The compiler can't report a missing ; after the } because it has no way to know there's supposed to be one there, since the Frame that comes after it is perfectly legal.
It's not unusual for a single missing ; or missing } to result in errors reported many lines later than the actual problem, sometimes hundreds of them.
Figured I'd report back to anyone that's interested. The problem was that the Frame class that was supposed to be definining mCameraFrame was in a different namespace, so all I had to do was "using namespace ....;". Doh! :P