extern const in c++

  • Thread starter Rolf S. Arvidson
  • Start date
R

Rolf S. Arvidson

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?
 
V

Victor Bazarov

Rolf S. Arvidson said:
[...]
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
 
M

Matthias Kaeppler

Rolf said:
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.
 
I

Ioannis Vranos

Rolf said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top