Thread: .h files: "constant already defined"

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    .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:
    Code:
    #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

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    In your .h files use

    header1.h
    #ifndef _HEADER1
    #define _HEADER1

    <conetnts of header1>

    #endif

    and

    header2.h
    #ifndef _HEADER2
    #define _HEADER2


    <contents of header2>

    #endif



    That's generally a good way to prevent files from being included twice.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you have declared a variable in a header file then that is like declaring it in every source file that includes the header file which is why you get an error telling you that it is allready defined.
    You have two options. If these are constants that will never change then you could consider using #define instead.
    If you realy want to make a variable accessable from multiple source files then you can use the extern keyword.
    in your header file
    Code:
    extern float MyVariable;
    In one source file
    Code:
    float MyVariable;

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    Quote Originally Posted by Maz
    In your .h files use

    header1.h
    #ifndef _HEADER1
    #define _HEADER1

    <conetnts of header1>

    #endif

    and

    header2.h
    #ifndef _HEADER2
    #define _HEADER2


    <contents of header2>

    #endif



    That's generally a good way to prevent files from being included twice.
    I have put it in every .h files but I get same error.
    I have:
    Code:
    #ifndef _CONSTANTS_H_
    #define _CONSTANTS_H_
    // definitions
    #endif

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    Quote Originally Posted by Quantum1024
    If you have declared a variable in a header file then that is like declaring it in every source file that includes the header file which is why you get an error telling you that it is allready defined.
    You have two options. If these are constants that will never change then you could consider using #define instead.
    If you realy want to make a variable accessable from multiple source files then you can use the extern keyword.
    in your header file
    Code:
    extern float MyVariable;
    In one source file
    Code:
    float MyVariable;
    I have to define variable two times? in header and in cpp files? It get value of variable in header file?
    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by wakeup
    I have to define variable two times? in header and in cpp files? It get value of variable in header file?
    Thanks
    Only in one of your cpp files.
    here's an example
    Code:
    //variable.h
    extern int MyVariable
    Code:
    //variable.cpp
    #include "variable.h"
    int MyVariable = 0;
    Then you would #include variable.h in every file that needs to access the variables.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The one in the header is called a "declaration", by the way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    the easiest and best way to do this is to declare the variable static const i.e.
    Code:
    //constants.h
    #ifndef _CONSTANTS_H_
    #define _CONSTANTS_H_
    
    static const int SOME_CONSTANT = 0;
    #endif
    1) you won't get linking errors.
    2) the variable is const so it's really a constant
    3) the declaration and value are in the same file so it's easier to maintain.
    Last edited by ChaosEngine; 11-21-2005 at 05:53 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Two minor notes about that. First, identifiers with underscores followed by capital letters are reserved according to the standard, so CONSTANTS_H_ would be better than _CONSTANTS_H_. Second, using static in that way has been deprecated by the standard, an unnamed namespace is preferred:
    Code:
    //constants.h
    #ifndef CONSTANTS_H_
    #define CONSTANTS_H_
    
    namespace
    {
      const int SOME_CONSTANT = 0;
    }
    
    #endif

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    Two minor notes about that. First, identifiers with underscores followed by capital letters are reserved according to the standard, so CONSTANTS_H_ would be better than _CONSTANTS_H_. Second, using static in that way has been deprecated by the standard, an unnamed namespace is preferred:
    Code:
    //constants.h
    #ifndef CONSTANTS_H_
    #define CONSTANTS_H_
    
    namespace
    {
      const int SOME_CONSTANT = 0;
    }
    
    #endif
    well I just copied his include guard to be honest.

    I wasn't aware that static was deprecated in this fashion. could you point me where in the standard it says this (note:I'm not disagreeing with you, I'd just like to know for my own information! cheers )
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Annex D, section 2:
    The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.5).
    Section 3.3.5 just describes namespace scope.

    >> well I just copied his include guard to be honest
    Understood. I meant to mention it before, but didn't think it was worthy of the thread derailment by itself.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There should be no need for either the static or the unnamed namespace - true constants are folded in by the linker, I believe. Besides, using either of these techniques wastes space because the constants are duplicated in each object file, and you get many copies in the final executable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  2. Header Files (.h)
    By Mach_ie in forum C Programming
    Replies: 5
    Last Post: 09-23-2003, 05:30 PM
  3. Cant Make heads or tails of .h files
    By D3T in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2002, 10:03 AM
  4. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 03:07 PM
  5. header .h files
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2001, 09:26 PM