delorie.com/archives/browse.cgi | search |
From: | horst DOT kraemer AT t-online DOT de (Horst Kraemer) |
Newsgroups: | comp.os.msdos.djgpp |
Subject: | Re: Values in header ?? (urgent) |
Date: | Sat, 19 Feb 2000 09:17:26 GMT |
Organization: | T-Online |
Lines: | 69 |
Message-ID: | <38adb3c9.101621263@news.btx.dtag.de> |
References: | <886bi1$2l0$1 AT cubacola DOT tninet DOT se> |
Mime-Version: | 1.0 |
X-Trace: | news07.btx.dtag.de 950951804 22308 0306239354-0001 000219 09:16:44 |
X-Complaints-To: | abuse AT t-online DOT de |
X-Sender: | 0306239354-0001 AT t-dialin DOT net |
X-Newsreader: | Forte Free Agent 1.11/32.235 |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Reply-To: | djgpp AT delorie DOT com |
On Sun, 13 Feb 2000 14:27:19 +0100, "Marcus" <mesthlm AT hem DOT passagen DOT se> wrote: > Why canīt I declare a value in a header without get the error "multiple > definiton" when I include the header several times? I have tried #ifndef but > NOTHING seems to work. > If the header looks like this: > #ifndef HEADER > #define HEADER > bool value; > #endif > This error appear: > Error: multiple definiton of 'value' > ...when I include the header two times or if I try to compile the header. > Can someone wise explain what's wrong? Thank you... These are two disctinct problems. The purpose of the include gard file x.h #ifndef HEADER #define HEADER /* contents */ #endif is to prevent multiple declarations when this header file would be #included several times into the _same_ source file. If for instance x.h. is #included into h1.h and h2.h and you include #include "h1.h" #include "h2.h" in some file. But it doesn't prevent a multiple definition when x.h is include in two distinct source files. The guard only works per source file. Your primary error is that you define a variable in a header file. This should *never* be done. If you include this header in two source files you have two definitions of a variable with the same name in two separate source. This leads to an error at link time (not at compile time because the compiler only sees one source file at a time). The correct procedure is: file x.h #ifndef HEADER #define HEADER extern bool value; #endif Note that this is only a declaration while bool value; without 'extern' is a definition. This x.h is going to be included in every source file which would use 'value'. Now in one and only one .cpp file, let's call it "implementation file", which is part of your project you define bool value /* = false */; Regards Horst
webmaster | delorie software privacy |
Copyright Đ 2019 by DJ Delorie | Updated Jul 2019 |