DukeOfMarmalade
DukeOfMarmalade

Reputation: 2788

C++ Fatal Error C1017

I have am getting:

fatal error C1017: invalid integer constant expression

with the following piece of code:

#if V1>0
//do stuff
#endif

V1 is defined as follows at the top of the .cpp file:

#define V3 UINT32
#define V4  sizeof(V3)
#define V1 (V2 % V4)

V2 is defined as follows:

const int V2 = 256;

Can anybody see where I am going wrong?

Upvotes: 1

Views: 2120

Answers (2)

Wug
Wug

Reputation: 13196

try

#define V2 256

instead of

const int V2 = 256;

Edit: There is some good reason you're doing preprocessor macros instead of just regular branching, right?

Edit 2: As someone else pointed out, the preprocessor does not accept sizeof statements, as these are handled by the compiler. Your options are to use a programmatic if statement (as opposed to a preprocessor one) or to use the real value of the size expression (which would involve hardcoding the size. However, since it's a UINT32, I don't think it will ever be anything other than 4.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258548

Preprocessing takes place before compilation. That means you can't use compile-time concepts (i.e. const int TOTAL = 16;) during preprocessing. The preprocessor has no knowledge of variables in your program (even const).

Instead of using a #define for the constant as well, as Wug suggested, a cleaner solution would be:

typedef UINT32 TARGET_TYPE;
const int TARGET_TYPE_SIZE = sizeof(TARGET_TYPE);
const int TOTAL = 16;
const int NUM_BYTES = (TOTAL % TARGET_TYPE_SIZE);

Upvotes: 4

Related Questions