#define???

Started by
26 comments, last by deadlydog 21 years, 4 months ago
quote:Original post by deadlydog
I don't understand it because it starts to say that I'm missing an ';' somewhere before I declare the const int blah, even though I haven't changed anything else.

Any solutions?? Thnx guys again.


I dug up the file you used from Tricks, demo7_13, and changed that one line. The program worked. I have a suggestion: make a new project workspace and start over with the original file found on the cd, and change that one line again. It should work if you only change that one line.

EDIT: okay, I didn't actually run the program, I just meant it compiled fine. When I did run it, I found that that was the program that refused to work on my computer properly.(ie, crap showed up on the screen) But anyway, the program should compile properly.

[edited by - nobodynews on December 7, 2002 2:15:37 PM]

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Here''s somthing not mentioned before:

if you want to use const declarations in header files, you must put the key static also in C++.
Otherwise the compiler might complain about duplicate definitions if you don''t use #pragma oce or #ifdef/#endif in your header files.

Regards,
Pat
quote:Original post by Miserable
It sounds like you don''t quite get what a C++ const variable does. There is no reason why one should have to go around changing every occurrence of them. If instead of
#define MAP_SIZE_X 100   

you have
const unsigned int MAP_SIZE_X = 100;   

you can use it in the exact same way. You want to change it? Still just one occurrence. You want to use it across several files? Still just one occurrence (using extern), which might not apply to #defines. The difference? Nothing much, really, except for type safety and the fact that it''s a real variable, so if for some odd reason you need to take its address, you can.

Edit: ... And C++ constants obey scope rules, which is useful.

[edited by - Miserable on December 7, 2002 8:46:22 AM]


Err, actually what I meant is how I''d use a constant over hard-coded values. I never mentionned #define specifically, didn''t I?

quote:What I said
I''d say the use of #declare over the C++ "const" is a matter of coding style and preference. Personally, I''d use a constant for easy modifications.


(and then some example of use of a constant (declared with #define in that case) followed)



And if you want to be clever, I don''t think you can use the C++ "const" to do something like...

#declare Begin {
#declare End }

(or something to that end). Teehee.
quote:Original post by RuneLancer
And if you want to be clever, I don''t think you can use the C++ "const" to do something like...

#declare Begin {
#declare End }

(or something to that end). Teehee.

No, you certainly can''t. I''m not sure I''d consider that particularly clever, though ... if you really want Begin ... End blocks, use Pascal or something. One potential pitfall of clever tricks (though it doesn''t apply here) is the possibility of having your clever #define clash with a variable name and get all sorts of unpredictable replacements (if you''re not careful).

But in any case, I shan''t deny that there may be cases where #defines are preferable to const variables (as I said in my first post, consts are usually better than #defines; I never said ''always''); I just can''t think of any, and in simple cases (as here), I''d recommend the const variable.
quote:Original post by deadlydog
Line 33 was the "const int SCREEN_WIDTH = 640;", and there is nothing before it except for all the #includes, so I don''t see why the errors would come up from simply using that line, instead of #define SCREEN_WIDTH 640.

Check that the header that is being included right before line 33 does not contain any syntax errors in the last few lines (such as missing semicolons).

If you still can''t get it to work you should post some code.


As has been said many times, #define is a dumb replacement of text.

believe it or not, thats an understatement. It is extremely literal.

Given this

#define SCREEN_WIDTH = 640

every time it sees "SCREEN_WIDTH", that gets replaced with "= 640", that can really screw up your code. When using #define DO NOT USE an "=" because it takes everything after it.

#define can do some really nice stuff if used properly, if not it can create havoc.

Take Squaring a number for example:
Correct:
#define SquareIt(n) ((n)*(n))
Wrong:
#define SquareIt(n) n*n
Given " X * SquareIt(Y + Z)/W;" It translates to "X * Y + Z*Y + Z/W"
#define SquareIt(n) ((n)*(n));
Given " X * SquareIt(Y + Z)/W;" It translates to "X * ((Y + Z)*(Y + Z));/W;"

And the #define''s work top down, so they can mess each other up if your not careful. Any time you use a #define, it should be well thought out, and clear as day to how your using it.

TIP:
Put all your defines in one place so they are easy to find, and reference. #pragma once ontop of all your files can help if you have a complicated project.


Compiling...
demo7_13.cpp
D:\My Documents\C++ Files\Source\T3DCHAP07\demo7_13.cpp(33) : warning C4091: '' : ignored on left of ''const int'' when no variable is declared
D:\My Documents\C++ Files\Source\T3DCHAP07\demo7_13.cpp(33) : error C2143: syntax error : missing '';'' before ''constant''
D:\My Documents\C++ Files\Source\T3DCHAP07\demo7_13.cpp(33) : fatal error C1004: unexpected end of file found
Error executing cl.exe

Given the second 2 errors, you may have a problem further up the line as well...Try formatting your code and looking for a missing }.
As humans we are all gods, just to different extents of power.
Also one use for const over #define would be when your debugging line-by-line and need to see how the variable you''re changing compares against a defined macro

the Debugger should give you a value with const int ScreenWidth = 600
if (x > ScreenWidth)
( ... )

ZoomBoy
Developing a iso-tile 2D RPG with skills, weapons, and adventure. See my old Hex-Tile RPG GAME, character editor, diary, 3D Art resources at Check out my web-site


quote:Original post by ZoomBoy
Also one use for const over #define would be when your debugging line-by-line and need to see how the variable you''re changing compares against a defined macro

the Debugger should give you a value with const int ScreenWidth = 600
if (x > ScreenWidth)
( ... )



"The debugger." Which one are we talking about here? All the ones I''ve delt with can expand macros in the similar manner.

Proper type checking (in C++) is probably the best reason to avoid them (usually). I don''t know many C coders that preach them over constants/functions either. However, if you''re not going to take advantage of the tools given to you to better organize these constants, a macro is probably better than a global variable.

This topic is closed to new replies.

Advertisement