Skip to main content
gamedev.net
🔒 Locked

Question about #define

Started by toddhd Jun 10, 2004 at 12:16 PM 8 replies 1.5k views
Original Post
toddhd
toddhd
Hopefully this is an easy one, I'm just confused... I have a file called global.h, that holds a few global variables and defines, etc. Every file in my project #include's global.h. One define is called ITEMNUM, and looks like this:
#define ITEMNUM 10
When I use ITEMNUM in a regular *.cpp file, it has no problems with it. For example, I use it in this loop:
for (int loop=0; loop < ITEMNUM; loop++)
But when I use it my Class file, the compiler says that it is an undeclared identifier.
    
#pragma once
#include ".\global.h"

class MyClass
{
public:
	MyClass(void);
	int Items[ITEMNUM];
}
Any ideas why this is occurring? What am I doing wrong? [edited by - toddhd on June 10, 2004 1:17:20 PM] [edited by - toddhd on June 10, 2004 1:39:47 PM]
-Todd"Windows dectected that your mouse has moved. Please reboot for the changes to take effect"
toddhd
toddhd

#pragma once
#include <windows.h> // Header File For Windows

#include <math.h> // Header File For Windows Math Library

#include <stdio.h> // Header File For Standard Input/Output

#include <gl\gl.h> // Header File For The OpenGL32 Library

#include <gl\glu.h> // Header File For The GLu32 Library

#include <gl\glaux.h> // Header File For The Glaux Library

#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <list>
#include ".\menu.h"
#include ".\character.h"
#include ".\findfilecontainer.h"
#include ".\item.h"

using namespace std;

#define GOBACK 100000
#define ITEMNUM 10

//Variable Def''s

extern HDC hDC; // Private GDI Device Context

extern HGLRC hRC; // Permanent Rendering Context

extern HWND hWnd; // Holds Our Window Handle

extern HINSTANCE hInstance; // Holds The Instance Of The Application


//And so on, variable and function declarations follow

//they are defined in global.cpp

-Todd"Windows dectected that your mouse has moved. Please reboot for the changes to take effect"
Anon Mike
Anon Mike
The most common things in these situations is that you''re picking up the wrong version of your include file, you''ve got some #ifdefs that are turning off your definitions but don''t realize it, or perhaps it''s defined to be something other than what you think it is.

The place to start is to look at the output of the preprocessor. In VC right click on the file (not the project) select properties, go to C/C++, Preprocessor, and select Generate Preprocessed File (with line numbers), then recompile just that file.

You''ll end up with a file that ends in .i. This is the output of the preprocessor with all macros expanded. You can then see exactly what is getting compiled as well as trace through the include files. Using the line numbers you can see what stuff is being skipped, etc.

Beware that if you include windows.h or something similiar the output will be huge. Your stuff will be at the end mostly.

Be sure to turn off Generate Preprocessed File when you''re done. Your code isn''t actually compiled when you have this option on, it''s just running the preprocessor.
-Mike
Aryoc
Aryoc
If you''ve happened to put the class file as one of the .h files in global.h, it''ll include that _before_ the #defines, and thus, ITEMNUM won''t be defined before it''s usage, but after, and so it''ll cause an error.

-Aryoc
-Aryoc
certaintragedy
certaintragedy
Just another side comment, but the "using namespace std;" line doesn't belong in a header file. Put that in your CPP files instead.

Also, it is considered better practice to use the form:
const ITEMNUM = 10;

rather than using a #define. #define simply replaces the text 'ITEMNUM' with '10' in your code, whereas the const ITEMNUM=10 format treats ITEMNUM more like a global variable.

[edited by - CertainTragedy on June 10, 2004 3:58:04 PM]
toddhd
toddhd
quote:
If you''ve happened to put the class file as one of the .h files in global.h, it''ll include that _before_ the #defines, and thus, ITEMNUM won''t be defined before it''s usage, but after, and so it''ll cause an error.

-Aryoc


I think you nailed it on the head. Is there a good way around this? Or do I just need to leave the class.h file out of the global.h and add it by hand? (Kinda defeats the purpose of the global.h, but then again, now I''m defeating the purpose of the #define).

-Todd"Windows dectected that your mouse has moved. Please reboot for the changes to take effect"
bytecoder
bytecoder
quote:

I think you nailed it on the head. Is there a good way around this? Or do I just need to leave the class.h file out of the global.h and add it by hand? (Kinda defeats the purpose of the global.h, but then again, now I''m defeating the purpose of the #define).


Don''t use global header files as a shortcut for including only the ones you need, they''re more ambiguous and cause your compile times to skyrocket.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.