need help with #Define...
is there any way to pass the value of an int into something that is under #define?
when i try the obvious (to a newb)...
//Begin of code
int nWhatever = 10;
#define WHATEVER = nWhatever
//End of code
i get errors further on down in my code.
if it helps im trying to pass a value from a file (using ifstream) into something under #define in the tut im using.
thanks in advance.
EDIT: Didn't know it made the Source code box so big if u put in something small...
[edited by - Shaun_30 on August 25, 2002 8:14:27 PM]
No. Use const. #define is nothing more than a macro. Before compiling, the compiler replaces every instance of 'WHATEVER' with '= nWhatever'. Needless to say, this causes issues.
[edited by - micepick on August 25, 2002 8:18:28 PM]
//Begin of codeint nWhatever = 10;const int WHATEVER = nWhatever;//End of code
[edited by - micepick on August 25, 2002 8:18:28 PM]
The actual answer to your question (tho you SHOULD be using consts) is that #define does not take the assignment operator. Try this.
#define WHATEVER nWhatever
Don''t listen to me. I''ve had too much coffee.
#define WHATEVER nWhatever
Don''t listen to me. I''ve had too much coffee.
quote: Original post by Sneftel
The actual answer to your question (tho you SHOULD be using consts) is that #define does not take the assignment operator. Try this.
#define WHATEVER nWhatever
No, I think he wants the VALUE of nWhatever to be "#defined". That macro is kinda pointless; it's just renaming. Besides, it's dangerous. What if he accidentally uses WHATEVER where nWhatever is out of scope? Worse, what if he uses it when a different nWhatever is in scope?
[edited by - micepick on August 25, 2002 8:24:57 PM]
#defines are old style C constant definitions passed to the preprocessor. They can only be assigned literal values such as 10, 12.7, 'v', etc. By using the method you described, you are suggesting to the compiler that WHATEVER is a variable, for if you change the value of nWhatever you will be changing the value of WHATEVER - this is not allowed and defeats the purpose of #defines defining constants.
Unless you are coding specifically in C, use 'const' as suggested by micepick.
Regards,
Mathematix.
[edited by - mathematix on August 25, 2002 8:26:25 PM]
Unless you are coding specifically in C, use 'const' as suggested by micepick.
Regards,
Mathematix.
[edited by - mathematix on August 25, 2002 8:26:25 PM]
As it turns out thanks to my dumb newbie ideas (i thought that i could have a const equal a user defined regular int) i still cant figure out my problem... to anyone that feels like they can help me ive posted the source code here...
http://www.geocities.com/jay_n_silentbob420/Maze.zip
i would appreciate it if somone could help me change the value of what #define SCREEN_HEIGHT, and #define SCREEN_WIDTH, to what is contained on the included map file (which can be viewed like a text file), note that I also threw in my beta map maker.
later.
[edited by - Shaun_30 on August 25, 2002 9:12:55 PM]
http://www.geocities.com/jay_n_silentbob420/Maze.zip
i would appreciate it if somone could help me change the value of what #define SCREEN_HEIGHT, and #define SCREEN_WIDTH, to what is contained on the included map file (which can be viewed like a text file), note that I also threw in my beta map maker.
later.
[edited by - Shaun_30 on August 25, 2002 9:12:55 PM]
Uhh, what''s the problem? What''s wrong with just using MapHeight and MapWidth as the dimensions?
Oh, I think I get it! You want to be able to allocate the screen buffer like so:
...right?...
Well, you can''t do it that way. That only works if the compiler can tell what SCREEN_WIDTH * SCREEN_HEIGHT is at compile-time. That''s impossible, since you can''t tell what they are until you run the program. You need to do this:
Was that the problem? If it was, read up on dynamic memory allocation. There''s hardly a way to program anything worthwile without it
CHAR_INFO screenBuffer[SCREEN_WIDTH * SCREEN_HEIGHT] = {0};
...right?...
Well, you can''t do it that way. That only works if the compiler can tell what SCREEN_WIDTH * SCREEN_HEIGHT is at compile-time. That''s impossible, since you can''t tell what they are until you run the program. You need to do this:
CHAR_INFO* screenBuffer = new CHAR_INFO[MapHeight*MapWidth];// and then when you''re done with screenBufferdelete[] screenBuffer;
Was that the problem? If it was, read up on dynamic memory allocation. There''s hardly a way to program anything worthwile without it
THANK YOU, with those new commands it works flawlessly....now all i got to do i set it up so that it works after receiving data from the file. Ill also take that advice on the dynamic memory allocation.
thanks again
peace
[edited by - Shaun_30 on August 25, 2002 10:18:08 PM]
thanks again
peace
[edited by - Shaun_30 on August 25, 2002 10:18:08 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement