• Advertisement

Archived

This topic is now archived and is closed to further replies.

#define and vc++ is messin' with my head

This topic is 6499 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

Okay, I''m screwing around with VC++ 6.0, trying to get a decent feel for it. This compiles fine: if(intZoomLevel == 1){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } However, if I do this: #define ZOOM_LOCAL 1; ... if(intZoomLevel == ZOOM_LOCAL){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } I get all these error messages: error C2143: syntax error : missing '')'' before '';'' error C2059: syntax error : '')'' warning C4390: '';'' : empty controlled statement found; is this the intent? error C2181: illegal else without matching if WTF?!?!? It doesn''t recognize the if anymore? Why not? Any and all help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Don't end your define with a semicolon, you should type like this:
#define ZOOM_LOCAL    1 

instead of
#define ZOOM_LOCAL    1;




Edited by - DerekSaw on 4/9/00 10:20:03 AM

Share this post


Link to post
Share on other sites
Yep, the thing is that anything with a # in front of it is not actually C code -- it''s a preprocesor directive that tells the compiler to do certain things. So a ; is not neccasary since the preproccesor doesn''t use it.

--TheGoop

Share this post


Link to post
Share on other sites
defines are always messy. this mistake happens to me all the time although i should know better
use constants instead

const int ciZoomLevel = 1;

Share this post


Link to post
Share on other sites
Perhaps constants *are* better. Except for one thing...they take up space just like variables, whereas #defines just replace text before compiling.

Share this post


Link to post
Share on other sites
merlin9x9, that actually isn't true in C++. Well, at least it shouldn't be true

Constants are 'filled in' at compile time (since they don't change), and therefore won't take up any resources.

Actually, using the (free) Borland C++ 5.5 compiler using a simple test program and examining the assembler source generated, it can be seen that the compiler uses the values of consts directly instead of allocating space in memory to hold the value.

Erik

Edited by - Erik Post on 4/9/00 3:31:07 PM

Share this post


Link to post
Share on other sites
Actually, there isn''t any size difference. Either way the number has to be put in memory, so there isn''t any space saving.

--TheGoop

Share this post


Link to post
Share on other sites
Oh, cool. I''d figured that was true but I''d read somewhere that they were implemented simply as variables except the compiler just made sure there weren''t any attempts to change it. Oh, well.

Share this post


Link to post
Share on other sites
Well, it''s legal to take the address of a const variable. i.e.
const int two = 2;
const int * two_ref = &two;
is valid code. In such cases the compiler is required to emit an entry in the data segment for the variable two. So consts variables will take up "additional" space in such an instance. Additional is in quotes, because, after all, you can''t do same thing with #define preprocessor directives.

Share this post


Link to post
Share on other sites

  • Advertisement