constant variables not working in header

后端 未结 10 1954
悲&欢浪女
悲&欢浪女 2020-11-30 20:10

if I define my constant varibles in my header like this...

extern const double PI = 3.1415926535;
extern const double PI_under_180 = 180.0f / PI;
extern cons         


        
相关标签:
10条回答
  • 2020-11-30 20:21

    An old question, indeed, but one useful answer is missing.

    It is possible to trick MSVC into accepting static constants in headers simply by wrapping those in a "dummy" class template:

    template <typename Dummy = int>
    struct C {
         static const double Pi;
    };
    
    template <typename Dummy = int>
    const double C<Dummy>::Pi = 3.14159;
    

    Now, C<>::PI can be accessed from elsewhere. No redefinition complains; constant is directly accessible in each compilation units without fancy link time optimization. Macro can be rolled out to further prettify this approach (even though macros are evil).

    0 讨论(0)
  • 2020-11-30 20:23

    You need to declare the contants in the header and then define them in one of your code files. If you do not declare them anywhere, then there is a linker error when it tries to tie the declaration to the actual definition. You can also get away with using #ifdef statements to have one definition within the header.

    Make sure they are declared in a header that is included by everyone that needs them and make sure they are defined exactly once.

    Jacob

    0 讨论(0)
  • 2020-11-30 20:24

    The extern storage class for them is almost certainly the cause of the problem you're seeing. If you remove it, the code will probably be fine (at least in this respect).

    Edit: I just noticed that you've tagged this as both C and C++. In this respect C and C++ are really quite different (but from the error messages, you're apparently compiling as C++, not C). In C++, you want to remove the extern, because (by default) const variables have the static storage class. That means each source file (translation unit) will get its own "copy" of the variable, and there won't be any conflict between definitions in different files. Since you're (probably) only using the values, not treating them as variables, having multiple "copies" won't hurt anything -- none of them will be allocated storage space.

    In C, extern is rather different, and removing the extern won't make any real difference, because they'll be extern by default. In this case, you really need to initialize the variables in exactly one place, and declare them extern in the header. Alternatively, you can add the static storage class that C++ will add by default when/if you remove the extern from the header.

    0 讨论(0)
  • 2020-11-30 20:26

    The problem is that you are initializing the variables in the header file; this creates a defining declaration, which is repeated in every file that includes that header,hence the multiple definition error.

    You want a non-defining declaration (no initializer) in the header file, and put the defining declaration in one of the implementation files.

    0 讨论(0)
  • 2020-11-30 20:28

    extern means the 'real' definition of the variable is elsewhere, and the compiler should trust that things will hook up at link time. Having the definition inline with the extern is weird and is what's munging up your program. If you want to have them be extern, just define them exactly once elsewhere in your program.

    0 讨论(0)
  • 2020-11-30 20:31

    in declaring global const within header causes that each compilation unit including this hader will have own definitions global definitions with the same name. Then linker does not like that.

    If You really need these in header then probably You should declare them as static.

    0 讨论(0)
提交回复
热议问题