Jump to content
  • Advertisement

Archived

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

toddhd

Question about #define

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement

#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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!