Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

.h files: "constant already defined"

Hi, I'm newbie in c++.
I have 2 cpp files, and their 2 .h files. I have another .h file with constant too.
The first cpp file use the second cpp file. And First ands second file are using constants .h file. In two cpp files I have #include "constants.h" line. When I compile I get this error:

Generating Code...
Linking...
Recristalazation_CEIT.obj : error LNK2005: "double QGB" (?QGB@@3NA) already defined in Functions.obj

One line for constant en constants.h

I have searched in google and I have probe this solution. In constants file I have written:

#ifndef _CONSTANTS_H_
#define _CONSTANTS_H_
// las definiciones
#endif

But I get the same error. How can I solve it?
Thanksss

PD: sorry my engish is very bad, I'm spanish

___________________________
Rap (http://www.hhdirecto.net)
[892 byte] By [wakeup] at [2007-11-11 7:19:59]
# 1 Re: .h files: "constant already defined"
You shouldn't define variables in a header file, because a heaer file is #included multiple times, in several different translation units. You should provide only declarations, not definitions. The definitions should appear in a single .cpp file that is compiled only once (i.e., never #include the .cpp file in another file!). So lets's summarize this:
constants.h -> declarations only, e.g., extern double MYDOUBLE;
constants.cpp ->definitions only: extern double MYDOUBLE=0.0;
other.cpp -> #include constants.h only.
Danny at 2007-11-11 21:02:29 >
# 2 Re: .h files: "constant already defined"
if you only need constant values, no need using real variables. you can use :
#define pi 3.14
( in a header with "include guard" ) and #include that header in your cpp files.
mr1yh1 at 2007-11-11 21:03:30 >
# 3 Re: .h files: "constant already defined"
I'm not so keen on using macros as constants because many compilers can't debug them properly and besides, real constants give your more control over the storage type: you can declare them volatile for instance, which you can't do with macros.
Danny at 2007-11-11 21:04:29 >