Macro issues in C++

user820304

I am trying to implement some code that is working for other people. I am using Visual Studio 2013, but I believe they're using a different compiler. Everything in the code is working properly expect a handful of macros.

One example:

#ifndef SAFE_DELETE
#define SAFE_DELETE(x) /
    if (x != NULL)        /
    {                    /
    delete x;     /
    x = NULL;         /
}
#endif

Here I get the error expected a declaration, or error C2059: syntax error : 'if'

Not surprisingly, I get an error when trying to use it:

SAFE_DELETE(*it); // "expected an expression"

The namespace where I define this macro is shared with the rest of my application, and the macro isn't used anywhere else. There also isn't any other syntax errors. Commenting out the include and SAFE_DELETE(*it); produces code that will compile. Additionally, replacing the macro call with the code from the macro also works.

I tried to re-define the function as follows:

#ifndef SAFE_DELETE
#define SAFE_DELETE(x) /
    x = NULL;         /
#endif

This produces the error Error: this declaration has no storage class or type specifier. That leads me to believe that somehow this isn't being called in a class, yet it very clearly is.

What could be causing this issue? Additionally, how is it possible that this compiled for someone else?

Brian Bi

You need a backslash (\) to continue a line, not a forward slash (/).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related