GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Mar-2006, 05:18
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

#ifndef not working?


Sorry C/C++ lovers for the provocative title, I know better than to blame the language for my mistakes.

Anyway, to avoid the "multiple definitions"-errors aswell as the "no definition"-errors for my classes, I tried to add this:

CPP / C++ / C Code:
#ifndef _DEFINITIONS_INCLUSION_
#define _DEFINITIONS_INCLUSION_
obj_database database;
obj_piece piece;
obj_board board;
obj_algorithms algorithms;
#endif

In all my .cpp files in a project. If I only have the definitions in main.cpp I get errors saying they're not defined. The problem is that the preprocessor doesn't seem to care about those instructions, including them in every file, causing the multiple definitions error.

I'm using Dev-C++, by the way.
  #2  
Old 04-Mar-2006, 08:02
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 958
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: #ifndef not working?


Is the error happening when the linker runs? If so, that would make sense because every object has these defined, and when the linker runs to link the objects together is were "multiple definitions" will occur.

When you only have them in main (for the not defined error) that typically means that it doesn't about the obj_* types to which you declare. This may mean two things:

1. you have this #ifndef block positioned in the file before the #include's that define the types, or
2. main has no clue (or use) of these declarations?

What are you trying to accomplish -- have one set of these declared globally somewhere? or something else?
  #3  
Old 04-Mar-2006, 08:35
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

Re: #ifndef not working?


Yes, the error occurs during linking and yes, I'd like to have these globally declared. I don't see however how that error makes sense. Only one file ought to have those objects declared unless the preprocessor doesn't keep the definitions when starting to work on another file.

Quote:
2. main has no clue (or use) of these declarations?
Well, main only uses one of them so far.

EDIT with startling discovery: I tried commenting (/* like this */) the
CPP / C++ / C Code:
#ifndef _DEFINITIONS_INCLUSION_
#define _DEFINITIONS_INCLUSION_
obj_database database;
obj_piece piece;
obj_board board;
obj_algorithms algorithms;
#endif
but the compiler doesn't seem to care about that and preprocesses it anyways. I had to delete the whole part for it not to multiple define again.
Leading to the not defined-error, of course.
  #4  
Old 04-Mar-2006, 08:58
davis
 
Posts: n/a

Re: #ifndef not working?


Quote:
Originally Posted by Honourable Mist
Yes, the error occurs during linking and yes, I'd like to have these globally declared. I don't see however how that error makes sense. Only one file ought to have those objects declared unless the preprocessor doesn't keep the definitions when starting to work on another file


Well, main only uses one of them so far.

CPP / C++ / C Code:
#ifndef _DEFINITIONS_INCLUSION_
#define _DEFINITIONS_INCLUSION_
obj_database database;
obj_piece piece;
obj_board board;
obj_algorithms algorithms;
#endif

I'd strongly recommend rethinking the use of globals, however, there is an easy solution.

CPP / C++ / C Code:
#ifndef _DEFINITIONS_INCLUSION_
#define _DEFINITIONS_INCLUSION_
extern obj_database database;
extern obj_piece piece;
extern obj_board board;
extern obj_algorithms algorithms;
#endif

...and then in your main:

CPP / C++ / C Code:
obj_database database;
obj_piece piece;
obj_board board;
obj_algorithms algorithms;


:davis:
  #5  
Old 04-Mar-2006, 09:25
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

Re: #ifndef not working?


Wow, I learn something new every time I come here, it seems. I didn't even know of that command. It even worked and stuff, thanks!
  #6  
Old 19-Feb-2007, 18:37
Kalvorod Kalvorod is offline
New Member
 
Join Date: Dec 2006
Posts: 28
Kalvorod is on a distinguished road

Re: #ifndef not working?


I am having the same problem, why is using globals so bad. Passing the same variables to every function is not only annoying and difficult to look at, but time consuming especially if you need to change the variable type.

In my program many of the .h files #include each other because they are all related. Isn't there any way to have global variables that they all use with being (*gasp*) global variables?

By the way your extern thing doesn't seem to work at all. I suppose I'll show the includes of my headers. Keep in mind, even though there is a file called globalvariables.cpp, I moved some of the globals into the files they are associated with (the item list must be used with Item.h, so it's declared in that file).

CPP / C++ / C Code:
// main.cpp //
#include <ctime>
#include <map>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
//#include "Item-Gen.h"
//#include "Equipment_Data-Gen.h"
#include "Equipment-Gen.h"
//#include "RANDOM.h"
#include <algorithm>
//#include "Monster_Data-Gen.h"
#include "Monster_Modifier-Gen.h"
//#include "globalvariables.h"

CPP / C++ / C Code:
#ifndef Monster_Modifier__
#define Monster_Modifier__

#include "Monster_Data-Gen.h"
#include <vector>
#include <iostream>
#include <string>

CPP / C++ / C Code:
#ifndef Monster_Base__
#define Monster_Base__

#include "Monster_Modifier-Gen.h"
#include <vector>
#include <iostream>
#include <string>
#include "globalvariables.h"

CPP / C++ / C Code:
#ifndef GLOBAL_VARS__
#define GLOBAL_VARS__

#include <ctime>
#include "RANDOM.h"

CPP / C++ / C Code:
#ifndef MAG_MOD_GEN__
#define MAG_MOD_GEN__


//#include "Item-Gen.h"
#include <string>
#include <vector>
#include <map>
#include "globalvariables.h"

CPP / C++ / C Code:
#ifndef ITEM_GEN__
#define ITEM_GEN__


#include <string>
#include <vector>
#include "Mag-Mod-Gen.h"

CPP / C++ / C Code:
#ifndef Equipment_Data_Gen__
#define Equipment_Data_Gen__


#include "Item-Gen.h"
//#include "Mag-Mod-Gen.h"
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
//#include "globalvariables.h"

CPP / C++ / C Code:
#ifndef Equipement_Gen__
#define Equipement_Gen__


#include <string>
//#include "Item-Gen.h"
//#include "Mag-Mod-Gen.h"
#include "Equipment_Data-Gen.h"
//#include "globalvariables.h"

WHY WON'T IT LINK!
Last edited by admin : 20-Feb-2007 at 00:06. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #7  
Old 20-Feb-2007, 09:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: #ifndef not working?


Quote:
Originally Posted by Kalvorod
I am having the same problem, why is using globals so bad. Passing the same variables to every function is not only annoying and difficult to look at, but time consuming especially if you need to change the variable type.
I respect your opinion. Here's my opinion: If I have a global variable used in many functions, and I change the variable type, how do I remember which ones use the variable and how do I make sure that I change whatever needs to be changed everywhere it needs to be changed? For me, debugging is more annoying than typing parameters into function calls.

I think that there are some places where globals are appropriate, but the "annoyingness" factor doesn't affect my personal decision to use globals. Your Mileage May Vary.
Quote:
Originally Posted by Kalvorod
In my program many of the .h files #include each other because they are all related. Isn't there any way to have global variables that they all use with being (*gasp*) global variables?
Variables can be shared among functions in the same source file by declaring them "static" within that file. They will not be visible outside that file. (The good points and bad points of having shared variables are now confined to that source file.) Note that if they are declared static in a header that is included by another file and that other file doesn't have variables with those names, you will have compiler warnings (or, in some cases, maybe even errors, depending on how strictlly you set your compiler flags).

Compilers that enforce current standards will not let you declare a function in a header as extern and then declare it static inside the file itself. You might get away with it, but why would you want to? It's either static or non-static.
Quote:
Originally Posted by Kalvorod
By the way your extern thing doesn't seem to work at all.
The "extern thing" shown in previous examples works as advertised if you follow the rules. If it doesn't work, then it's your "extern thing" that doesn't work, not anyone else's. I give an example that works (for me) in my response to your post in the C++ forum. I show all files in a project, and indicate how to compile and link them. I try to list the rules. If you don't understand the reason for the rules, or you think it should be different, I'm sorry. They are still the rules.

There are other ways to get things to work, but if you start with something that works and you change it and it doesn't work, then you are in a pretty good position: you have a fallback position that works.

My suggestion: start with an example that works. Modify it to meet your needs. Then its: Onward and Upward!

Regards,

Dave

"Laissez les bons temps rouler!"
  #8  
Old 20-Feb-2007, 19:02
davis
 
Posts: n/a

Re: #ifndef not working?


I don't know how to improve upon what Dave said except maybe to explain some of the reasons why globals should be avoided. However, these are nothing new. One thing that I would like to add is that novices write software that attempts to solve a problem. Experienced programmers try to write software that solves problems, but also fits inside their "tool boxes" so that they have reusable elements of code that accomplish functionality so that they don't have to rewrite the same code over again the next time that they need it.


:davis:
  #9  
Old 20-Feb-2007, 22:38
Kalvorod Kalvorod is offline
New Member
 
Join Date: Dec 2006
Posts: 28
Kalvorod is on a distinguished road

Re: #ifndef not working?


Thanks for posting both of you. I've since learned that's it's not the ifndefs that aren't working because I didn't realize that they actually don't do what I thought they did. I can understand why writing global variables is "bad" but it seems to me to just be a convention thing. If you think about it, why would globals exist if they weren't meant to be used? However I do see your point on reusability, and long compile times (I've seen true programs take upwards of ten minutes per compile). However for what I'm doing, as a novice, a compile takes about 3 seconds, and I doubt I'll be using this code in any other program, as by the time I decide to start any program similar, I would have taken more classes and most likely learned much better ways of doing things.(The program I'm working on actually is in it's second version. The first used primitive arrays, now I have it using vectors and maps. You see what I mean.)

However thank you again for clearing some of that up. I seem to get a lot of criticism for using globals, yet again, for my program, it suits me much better. May it now be said that I never have used globals in any other program I've done, except for test programs in an entry level computer science class.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help! my HP ScanJet 3400c isnt working!! dora Computer Hardware Forum 3 04-Oct-2005 12:02
Fl_Help_Dialog Not working Dhaval FLTK Forum 1 08-Aug-2005 19:51
Re: Still working on the BBCodes... admin GIDNetwork™ 2 11-May-2005 22:28
Circular Linked Queue Copy Constructor and Assignment Operator Not Working? wc3promet C++ Forum 0 17-Oct-2004 07:55
PHP mail() not working sajuat MySQL / PHP Forum 1 22-Jul-2004 07:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.