#### Archived

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

# #define???

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

## Recommended Posts

Hi, I''m just learning direct x programming from my book I bought, "Tricks of the Windows Game Programming Gurus" right now, and for a lot of things he uses C code. I however have never taken C, I''ve only taken C++. He declares many things using #define. Now, I know what this does, but when I try changing his C code to C++, I get errors. For example, where he would declare: // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" // default screen size #define SCREEN_WIDTH 640 // size of screen #define SCREEN_HEIGHT 480 #define SCREEN_BPP 8 // bits per pixel I tried using things like: string WINDOW_CLASS_NAME = "WINCLASS1"; and, const int SCREEN_WIDTH = 640; and so on, except whenever I changed any of them, I would get errors. His code works fine the way it is, so I''m not sure what the problem is when I try and use C++ code instead. So my question is, is it possible to code this stuff using C++, and if so how, or what am I doing wrong?? Thnx - Dan He who laughs, lasts

##### Share on other sites
#define is a c++ command also, it is used to create a unchangeable value(program wise) that you can easily edit in your code if needed. They are not ment to be variables like you tried to make them.

##### Share on other sites
First, here''s an important engineering principle that will save you untold grief throughout your life:

"If it ain''t broke, don''t fix it."

Now to your problem. The STL string type is not equivalent to an array of chars, so you will get errors if a C function is expecting "char *" and you pass it a string. The "const int" substitution should work though -- what exactly is the error?

##### Share on other sites
quote:
Original post by Anonymous Poster
#define is a c++ command also, it is used to create a unchangeable value(program wise) that you can easily edit in your code if needed. They are not ment to be variables like you tried to make them.

#define is a relic from the days of C. It has its uses, but const variables in C++ are better, because they are type-safe. #define macros rely on dumb and blind text replacement.

This is not to say that they aren''t useful - to C programmers. In C++, you''re usually better off using const variables and inline functions.

As for the error, the second AP is right.

##### Share on other sites
quote:
Original post by Miserable
[quote]Original post by Anonymous Poster
#define is a c++ command also, it is used to create a unchangeable value(program wise) that you can easily edit in your code if needed. They are not ment to be variables like you tried to make them.

#define is a relic from the days of C. It has its uses, but const variables in C++ are better, because they are type-safe. #define macros rely on dumb and blind text replacement.

This is not to say that they aren''t useful - to C programmers. In C++, you''re usually better off using const variables and inline functions.

As for the error, the second AP is right.

I have a question, just as a newb to all of this. If #define does a text replacement, and that is a preprocessor action, won''t the compiler still catch it?

For example:

  #define MyCharacter xchar x = ''a'';int y = MyCharacter;

Won''t the preprocessor replace "MyCharacter" with "x", and then won''t the compiler still catch the error?

##### Share on other sites
In this case, the compiler will see,

    char x = 'a';    int y = x;

This is all handled by the pre-processor, so the compiler won't issue a warning.

It really is dumb text replacement.

[edited by - rotos on December 6, 2002 11:46:16 AM]

##### Share on other sites
But if the compiler sees:

int y = x;

Won''t it say "Hmmm...well x is a char" and then shoot an error back? I don''t mean to be difficult. I honestly don''t know.

##### Share on other sites
#define is very useful when used correctly. Eg: i fyou have a value that never changes throughout your code, it is more readable to use a #define and also gives you the ability to change every instance of that value by simply changing the #define itself.

As for the quote "#define is a relic from the days of C" I can verify that C is alive and well in the commercial games industry! Too many people get caught up with "C++ is better than C" etc...when people should be looking at the application of the language to their problem.

But anyway! You may very well be better off using const var''s instead, I just wanted to point out the positive use of #defines for a newbie perspective.

##### Share on other sites
quote:
Original post by Utwo
But if the compiler sees:

int y = x;

Won''t it say "Hmmm...well x is a char" and then shoot an error back? I don''t mean to be difficult. I honestly don''t know.

It probably won''t. char is often used as small integer, so conversion to another integer type should be silent.

##### Share on other sites
....this should''ve been in my ast post!...

the compiler will probably see the ASCII value of ''x'' assign that to y, which would be reasonable. Only in y it would be a int value rather than a char. Hope that makes sense :op

##### Share on other sites
Man, I tried posting this 6 different times. I wonder if this forum might have a concurrency issue?

Anyway, hello deadlydog. Whether #defines are "archaic" in C++ or not, it's kind of hard to tell exactly what your problem is w/o knowing what type of errors your getting. Are they compile-time or link-time errors? Posting a few would help (along with some more extensive code snippets.)

And where are the #defines you're trying to replace? In a header file (.h) or elsewhere?

[edited by - darrell l on December 6, 2002 12:51:16 PM]

##### Share on other sites
quote:
string WINDOW_CLASS_NAME = "WINCLASS1";

that won't work at all if the function/method expects a c-style string. if you do this:

const char WINDOW_CLASS_NAME[] = "WinClass1";

I believe you will get the results you are looking for(for that case). If you insist on using strings, there's a method of the string class which will return a c-style string version of the string, but I can't remember what it is.

EDIT: found the method:

string s = "somestring";
s.c_str(); // returns a c-style string

[edited by - nobodynews on December 6, 2002 3:04:18 PM]

##### Share on other sites
quote:
Original post by Anonymous Poster
#define is very useful when used correctly. Eg: i fyou have a value that never changes throughout your code, it is more readable to use a #define and also gives you the ability to change every instance of that value by simply changing the #define itself.

As for the quote "#define is a relic from the days of C" I can verify that C is alive and well in the commercial games industry! Too many people get caught up with "C++ is better than C" etc...when people should be looking at the application of the language to their problem.

But anyway! You may very well be better off using const var''s instead, I just wanted to point out the positive use of #defines for a newbie perspective.

The readability issue is pure opinion; I won''t comment further on it.

I''m well aware that C is alive and well, nor did I claim otherwise. What I did say is that in C++, #defined constants are C relics. (By the way, I''m not a C programmer, but didn''t they introduce the const keyword in more recent versions of ANSI-C? If so, then that has interesting implications.)

As for values that never change throughout your code - so what? That''s exactly what the const keyword is for - constants. Use a global const (well, globals aren''t good, but for the closest equivalent ...) and to change all occurrences, you''d still only change one instance. The only real difference is that you have a real variable with a type and an address.

##### Share on other sites
Hm, not to change the subject from the question at hand but...

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. For instance...

#define MAP_SIZE_X 100
#define MAP_SIZE_Y 250

(blah)

for(i = 0; i < MAP_SIZE_X; i++)
for(j = 0; j < MAP_SIZE_Y; j++)
DoMapStuff(i, j);

That way you don''t have to go through the code to change every instance of ''100'' and ''250'' if you decide those values aren''t right for your game, for instance.

What''s the difference between #define and const, in such a case? Result-wise, that is. Frankly, I can''t really see any, though I''ve never really caught on to a good deal of C++-specific things and prefer, say, #define over const, so I''m somewhat biased. ;P

But anyways. Back to deadlydog''s question.

What errors are you getting when you try to run it with your code? It could always be that since #define is just a text-replacement directive, some place where a value is expected that isn''t an int ends up receiving an int and thus, the compiler bitches (pardon the expression). *shrugs*

##### Share on other sites
Hey everyone, sorry about taking so long to reply, but thanks for all of your fast replies.

As for why I want do not want to use #define, it's just simply because I'd like to keep things consistant in my program. ie not use #define sometimes and const blah another time.

Here's the compile time error I get when trying to use:

const int SCREEN_WIDTH = 640;

#define SCREEN_WIDTH = 640

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.

demo7_13.obj - 2 error(s), 1 warning(s)

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.

- Dan
He who laughs, lasts

[edited by - deadlydog on December 7, 2002 3:35:58 AM]

##### Share on other sites
const int SCREEN_WIDTH = 640; looks right. What''s on line 33? There is of course the chance that you are missing a semicolon before line 33, and the compiler is treating line 33 as part of that line since there is nothing seperating them.

##### Share on other sites
quote:
Original post by RuneLancer
Hm, not to change the subject from the question at hand but...

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. For instance...

#define MAP_SIZE_X 100
#define MAP_SIZE_Y 250

(blah)

for(i = 0; i < MAP_SIZE_X; i++)
for(j = 0; j < MAP_SIZE_Y; j++)
DoMapStuff(i, j);

That way you don't have to go through the code to change every instance of '100' and '250' if you decide those values aren't right for your game, for instance.

What's the difference between #define and const, in such a case? Result-wise, that is. Frankly, I can't really see any, though I've never really caught on to a good deal of C++-specific things and prefer, say, #define over const, so I'm somewhat biased. ;P

But anyways. Back to deadlydog's question.

What errors are you getting when you try to run it with your code? It could always be that since #define is just a text-replacement directive, some place where a value is expected that isn't an int ends up receiving an int and thus, the compiler bitches (pardon the expression). *shrugs*

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]

##### Share on other sites
quote:
const int SCREEN_WIDTH = 640; looks right. What's on line 33? There is of course the chance that you are missing a semicolon before line 33, and the compiler is treating line 33 as part of that line since there is nothing seperating them.

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.

- Dan
He who laughs, lasts

[edited by - deadlydog on December 7, 2002 12:16:37 PM]

##### Share on other sites
Goy, thats a toughie, how about posting up the first 30 or so lines of your source code?

##### Share on other sites
Is it possible that the line directly before it is:
using namespace std
?
Perhaps without the semicolon just as I just wrote it?...It needs a ; of course. Just one idea...Hard to help without seeing all the code

##### Share on other sites
quote:
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]

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
quote:
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.