?extern?

Pages: 12
Hi all,

Please take a look at the following:

Header file:
1
2
3
...
extern const int nFint;
...


Cpp file (header file included in cpp file):
 
const int nFint = m;


m is a number and it is not zero.

Again the same cpp file but inside a function to test:
1
2
cout << m     << endl;
cout << nFint << endl;


Output:

m = 180
nFint = 0


Any idea?

Regards.
Last edited on
You can't change a constant number. It's constant. (That means unchanging)
I tried that. I mean I converted it from const int to just int but still gives the same output.
Last edited on
How is 'm' defined? If it's global you might be seeing the static initialization order fiasco.
m is defined at the top of the cpp file. Its scope is limited to that cpp file.

1
2
3
4
5
6
7
8
9
#include "..."
	
int m = 0;
...
fnc ()
{
     cout << m     << endl;
     cout << nFint << endl;
}
Last edited on
er... wait...

 
int m = 0;


m is zero.

so....

 
const int nFint = m;


Of course nFint will be zero. What else were you expecting? nFint won't change when m changes, so if you change m to something else later, nFint will still be zero.
Last edited on
Okay, what's going on is obviously not in the portion of code that you've posted. We need more verbose information...like everything that you're doing that has any effect on m of nFint. Assignments, functions, operations, everything.

er... wait...


int m = 0;


m is zero.

so....


const int nFint = m;


Of course nFint will be zero. What else were you expecting? nFint won't change when m changes, so if you change m to something else later, nFint will still be zero.


m has changed later. I did not write that because I put the output. So m has changed to 180 but assingment to nFint doesn't work.


Okay, what's going on is obviously not in the portion of code that you've posted. We need more verbose information...like everything that you're doing that has any effect on m of nFint. Assignments, functions, operations, everything.


I don't see any external process but I'll check again.
m has changed later. I did not write that because I put the output. So m has changed to 180 but assingment to nFint doesn't work.


This is exactly what I was referring to.
Last edited on
I think it is because "const int nFint = m;" is never compiled because it is not in a function. But when I put the assingment inside a function it gives LINK error. For example, if I put the assingment inside fnc1 (see below) then it will be compiled but as I said gave LINK error in this case.


Header:
1
2
3
4
5
extern const int nFint;
...
void fnc1 ();
void fnc2 ();
void fnc3 ();



Cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "..."
	
int m = 0;
...

fnc1() {...}
fnc2() {...}

const int nFint = m;

fnc3 ()
{
	fnc1 ();
	fnc2 ();	
}
We already fixed your problem. The problem was
1) You can't change a const.
2) You changed m without telling us about it.
We already fixed your problem. The problem was
1) You can't change a const.
2) You changed m without telling us about it.


:) Because I posted the output in the first post I expected from you to understand that m is changing. OK, my fault. But as I said I also tried without const and did not work.
Well, we still haven't seen all of the operations that you perform or attempt on each of the variables.
Even if nInt wasn't const, it won't change values along with m unless you explicitly set it equal to m every time m changes.
Well, we still haven't seen all of the operations that you perform or attempt on each of the variables.


The code is very long to put here. I'll try alone. Thanks to everybody who has contributed.
Even if nInt wasn't const, it won't change values along with m unless you explicitly set it equal to m every time m changes.


This.

It doesn't matter if you change m, nFint is initialized with whatever m at the time of initialization. Changing m after that means absolutely nothing to nFint.

If you want nFint to "echo" m, then you want to make it a reference:

 
const int& nFint = m;  // <- note the & to make it a reference 


This way, nFint and m are the same variable, so they will always equal each other.
Last edited on
const int& nFint = m;


That's a neat trick! I need to remember that.
Disch, you are great! I got the point now.
I know I bother you very much but let me ask the last one.

const int & nFint = m; ---> OK
const int & nFsur = p; ---> OK
const int & nFact = m+p; ---> This doesn't work.
Yeah, you can't make a reference to an arithmetic operation.
Pages: 12