![]() |
|
#1
|
|||
|
|||
#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:
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
|
||||
|
||||
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
|
|||
|
|||
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:
EDIT with startling discovery: I tried commenting (/* like this */) the CPP / C++ / C Code:
Leading to the not defined-error, of course. |
|
#4
|
|||
|
|||
Re: #ifndef not working?Quote:
I'd strongly recommend rethinking the use of globals, however, there is an easy solution. CPP / C++ / C Code:
...and then in your main: CPP / C++ / C Code:
:davis: |
|
#5
|
|||
|
|||
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
|
|||
|
|||
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:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
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
|
|||
|
|||
Re: #ifndef not working?Quote:
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:
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:
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
|
|||
|
|||
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
|
|||
|
|||
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 GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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