extern const in c++

Discussion in 'C++' started by Rolf S. Arvidson, Mar 4, 2005.

  1. Be kind, a newb question here. I don't understand why I get a compile-time
    error from the following:

    I define a variable in main.cpp as such

    double x = 1.;
    int _tmain(int argc, _TCHAR* argv[]){
    ...;
    }


    In separate files that main calls, I have access to this global variable x,

    extern double x;
    void file2(){

    x = szVi - 2.

    }

    No problem. However, I want to make this to be a constant, not a variable,
    to ensure it's never alered by mistake.
    But if I change the definition in main to extern,
    const double x = 1.;
    int _tmain(int argc, _TCHAR* argv[]){
    ...;
    }

    the linker can't resolve it:

    error LNK2001: unresolved external symbol "double szVi" (?x@@3NA)

    What am I doing wrong? How do I make this const visible to all files that
    need it?
     
    Rolf S. Arvidson, Mar 4, 2005
    #1
    1. Advertising

  2. "Rolf S. Arvidson" <> wrote...
    > [...]
    > But if I change the definition in main to extern,
    > const double x = 1.;
    > int _tmain(int argc, _TCHAR* argv[]){
    > ...;
    > }
    >
    > the linker can't resolve it:
    >
    > error LNK2001: unresolved external symbol "double szVi" (?x@@3NA)
    >
    > What am I doing wrong? How do I make this const visible to all files that
    > need it?


    Declare it 'extern' in 'main.cpp' as well:

    extern const double x = 1.;

    ....

    And keep in mind when posting here that C++ does not have '_tmain' function.
    'main' is what the Standard requires for every C++ program to have. If you
    happen to use Microsoft products for your studies, you should know that they
    do support regular C++ language, without extensions, and that's what you
    really want to learn, trust me.

    V
     
    Victor Bazarov, Mar 4, 2005
    #2
    1. Advertising

  3. Rolf S. Arvidson wrote:
    > Be kind, a newb question here. I don't understand why I get a compile-time
    > error from the following:
    >
    > I define a variable in main.cpp as such
    >
    > double x = 1.;
    > int _tmain(int argc, _TCHAR* argv[]){
    > ...;
    > }
    >
    >
    > In separate files that main calls, I have access to this global variable x,
    >
    > extern double x;
    > void file2(){
    >
    > x = szVi - 2.
    >
    > }
    >
    > No problem. However, I want to make this to be a constant, not a variable,
    > to ensure it's never alered by mistake.
    > But if I change the definition in main to extern,
    > const double x = 1.;
    > int _tmain(int argc, _TCHAR* argv[]){
    > ...;
    > }
    >
    > the linker can't resolve it:
    >
    > error LNK2001: unresolved external symbol "double szVi" (?x@@3NA)
    >
    > What am I doing wrong? How do I make this const visible to all files that
    > need it?
    >
    >


    'const' ist part of the type signature of 'x'. Whenever you change it
    from double to const double, you also have to declare it (using extern)
    as const double. Otherwise the types mismatch and the compiler can't
    figure out which variable (or constant) you mean.

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Mar 4, 2005
    #3
  4. Rolf S. Arvidson wrote:

    > Be kind, a newb question here. I don't understand why I get a compile-time
    > error from the following:
    >
    > I define a variable in main.cpp as such
    >
    > double x = 1.;
    > int _tmain(int argc, _TCHAR* argv[]){
    > ...;
    > }
    >
    >
    > In separate files that main calls, I have access to this global variable x,
    >
    > extern double x;
    > void file2(){
    >
    > x = szVi - 2.
    >
    > }
    >
    > No problem. However, I want to make this to be a constant, not a variable,
    > to ensure it's never alered by mistake.
    > But if I change the definition in main to extern,
    > const double x = 1.;
    > int _tmain(int argc, _TCHAR* argv[]){
    > ...;
    > }
    >
    > the linker can't resolve it:
    >
    > error LNK2001: unresolved external symbol "double szVi" (?x@@3NA)
    >
    > What am I doing wrong? How do I make this const visible to all files that
    > need it?




    When you define a variable e.g. double x= 1; it has by default external
    linkage, that is it is the same as if you defined extern double x= 1;


    However according to ISO C++ rules, const definitions have internal
    linkage, so const double x= 1; is as if you define it static const
    double x= 1; (or in an anonymous namespace). So if you want your const
    to have external linkage, you will have to use the keyword extern
    explicitly.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Mar 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,558
    tom_usenet
    Aug 2, 2004
  2. Replies:
    11
    Views:
    1,152
  3. Javier
    Replies:
    2
    Views:
    627
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,174
    Tim Rentsch
    Nov 10, 2008
  5. Andre
    Replies:
    5
    Views:
    567
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page