Public Group

Archived

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

#define???

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

Recommended Posts

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

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 56
• 17
• 27