Jump to content
  • Advertisement
Sign in to follow this  

Preprocessor failure?

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've got a kind of weird (to me, its weird) problem. I define a few words and then want to use them. So to combine the LEFTBOTTOM with the LEFTTOP I use a bitwise or, | . The problem is, the preprocessor doesn't do, what it is supposed to do. (or isn't doing what I think it should be doing ;) ) I think this small program will make it clear:
#define TOPLEFT         0x01;
#define TOPRIGHT        0x02;
#define RIGHTTOP        0x04;
#define RIGHTBOTTOM     0x08;
#define BOTTOMRIGHT     0x10;
#define BOTTOMLEFT      0x20;
#define LEFTBOTTOM      0x40;
#define LEFTTOP         0x80;

int main()
  char a = 0x40 | 0x80;           // <--- this is OK
  char b = LEFTBOTTOM | LEFTTOP;  // E2188: expression syntax error
  return 0;

If the precessor replaces all the defines, it is exactly the same as char a = 0x40 | 0x80 (the previous line) and should not throw me an error. Any help is appreciated. Thanks in advance. Kamos

Share this post

Link to post
Share on other sites
You'd probably be better off using const variables instead of #define macros. It gets around the invisible preprocessor (invisible in that the compiler doesn't catch errors until after the preprocessor does its work).

Share this post

Link to post
Share on other sites
Ah ofcourse!
And yes I guess const variables would be better, but it introduces a lot of work...
Say I'd use a const variable. When I use 2 classes, which make use of these const variables, I will have to declare these const variables twice. (once in each class).
This is double work. (and gets worse when using more classes)
Or am I wrong?

The preprocessor allows me to define names and use the defined names anywhere I want. (with the side effect of invisible errors...)

Share this post

Link to post
Share on other sites
You can put const static int TOPLEFT = 0x01; in a header file (not in a class definition) and it'll be available anywhere that header file is included.

For that matter, an enumeration might be more appropriate to what you're doing.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!