problem with extern and global variable

I am trying to use a variable which needs to be accessed across multiple files:
Here is how I have it defined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Data.h

#ifndef DATA_H_
#define #ifndef DATA_H_

#pragma once

class Data
{
public:
     struct Types{
          static const int Type1= 1;
	 static const int Type2= 2;
	 static const int Type3= 3;
	 static const int Type4= 4;
	 static const int Type5= 5;
	 static const int Type6= 6;
	 static const int Type7= 7;
	 static const int Type8= 8;
    };
};

  extern int Index; 

#endif

//Calcs.h

#ifndef CALCS_H_
#define CALCS_H_

#pragma once

#include <windows.h>

class Calcs
{
public:
  static void Sub1(int Num);
  static void Sub2();
};


#endif // CALCS_H_

//Main.cpp

#include <iostream>
#include <stdio.h>
#include "Data.h"

using namespace std;

int main()
{
    int Index;

    Index = 3;
    printf("\nThe index is %i \n",Index);
    cout<<"Setting up Index. \n"<< endl;
    Calcs::Sub1(Index)
}

//Calcs.cpp

#include <string>
#include <iostream>
#include <stdio.h>
#include "Data.h"
#include "Calcs.h"

using namespace std;

void Calcs::Sub1(int Num){

 cout<< "Num is " << Num << ".\n";
 cout<< "Data::Index is " << Index << ".\n";
}


When I build this code, the compiler gives me "undefined reference to 'Index'" error. I tried all different combinations of defining as various ways.When I change the declaration of RefrigerantIndex in Data.h from
extern int Index; to static int Index;, it compiles and builds well. I dont know which is the right way to do.

I would appreciate your help in getting this matter resolved. I can keep this as static and go on for now, but I would like to build the code in the best possible way byt understanding what I am writing. In addition to help, if any one can suggest excellent references (video tutorials) to understand various concepts that would be very helpful.

Regards
Sankar
At line 23 you're saying that index had global scope. e.g. ::Index
At line 56, index has local scope. e.g. main::Index.
To the lnker, these are two different variables.

Move line 56 to global scope (line 53) and you should be fine.
Thanks Anon,

That worked.

I now see what the scope of the variable is. Do you have any recommendation on the advantages and disadvantages on this kind of usage. I read that use of global variables is not a really good way to program in C++.

If this is not the best way to program the above task, it would be great if you could (if you have time) show us how could this program be made better.

Thanks
You're correct that it's best to avoid global variables.

Looking more closely at you program, I don't see that you have any need for the global instance of Index. I would just eliminate the global instance (and it's extern) and leave the local instance of inside main.
Topic archived. No new replies allowed.