Global Variables with Headers

Sep 1, 2010 at 7:47am
Hi,I have a problem with global variables in my header file.Even after looking through many articles regarding this issue,I am still unable to solve the problem.I'm just a student learning C++ in my school and thus this is all quite new to me.Regarding the program,it is a project me and my team are suppose to do,there are many inefficient methods and steps,but it is part of the learning process our teacher wants us to go through.This is my first post so bear with me.

This is my attempt to fix the problem with some suggestions I found.
I guess I do not fully understand it but it involves using extern for my global variables and then declaring them in my cpp files.

This is my header file.
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
//Class Definition.h
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

	extern int one=0;
	extern int two=0;
	extern int three=0;
	extern string n1;
	extern string n2;
	extern string n3;
	extern string id;
	extern string n;
	extern string ad;
	extern string hp;


class Mem_Particulars
{
private:
	string idNum;
	string name;
	string address;
	string hpno;
	string check;
public:
	void setID_Name_ad_hp(string id, string n,string ad,string hp);
	void showData(void);
    void write_in_data(void);
};
class Nom_Particulars
{
protected:
	string idNum;
	string name;
	string address;
	string hpno;
public:
	void nom1_setID_Name_ad_hp(string id, string n1,string ad,string hp);
	void nom1_showData(void);
	void nom1_write_in_data(void);
	void nom2_setID_Name_ad_hp(string id, string n2,string ad,string hp);
	void nom2_showData(void);
	void nom2_write_in_data(void);
	void nom3_setID_Name_ad_hp(string id, string n3,string ad,string hp);
	void nom3_showData(void);
    void nom3_write_in_data(void);
	void assign_nom(void);
};
class voting_system : protected Nom_Particulars
{
private:
    int nom1pt;
	int nom2pt;
	int nom3pt;
public:
	voting_system(int a=0, int b=0, int c=0)
	{
		nom1pt = a;
	    nom2pt = b;
	    nom3pt = c;
	};
    void display_points(string n1,string n2,string n3,int a,int b,int c);
    void voting_option_1stplace(string n1,string n2,string n3,int a,int b,int c);
	void voting_option_2ndplace(string n1,string n2,string n3,int a,int b,int c);
	void voting_option_3rdplace(string n1,string n2,string n3,int a,int b,int c);
	~voting_system(){};
};

Sep 1, 2010 at 7:49am
This is my function definition cpp file.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "Class Definition.h"
void voting_system::voting_option_1stplace(string n1,string n2,string n3,int a,int b,int c)
{
	int vote;
	int choice;
	cout<<"You Are Voting for President of the club"<<endl;
	cout<<"Would You Like To Vote?"<<endl<<endl;
 cout<<"1=Yes, 2=No : "; 
 cin>>vote;
 if (vote == 1)
 { 
 cout<<"\nThese are the nominees:"<<endl<<endl;
 cout<<"1. ="<<n1<<endl;
 cout<<"2. ="<<n2<<endl;
 cout<<"3. ="<<n3<<endl;
 
ask:
 cout<<"Enter Your Choice Here: ";
 cin>>choice;
 if (choice == 1)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n1<<endl;
 one=one+5;//nom1 gets 5 points
 }
 else if (choice == 2)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n2<<endl;
 two=two+5;//nom2 gets 5 points
 }
 else if (choice == 3)
 {
 cout<<""<<endl;
 cout<<"You voted "<<n3<<endl;
 three=three+5;//nom3 gets 5 points
 }
 else
 {
 cout<<"You Didn't Enter An Available Option"<<endl;
 goto ask;
 }
}
}
 void voting_system::voting_option_2ndplace(string n1,string n2,string n3,int a,int b,int c)
{
	int vote;
	int choice;
	cout<<"You Are Voting for 1st Committee member of the club"<<endl;
	cout<<"Would You Like To Vote?"<<endl<<endl;
 cout<<"1=Yes, 2=No : "; 
 cin>>vote;
 if (vote == 1)
 { 
 cout<<"\nThese are the nominees:"<<endl<<endl;
 cout<<"1. ="<<n1<<endl;
 cout<<"2. ="<<n2<<endl;
 cout<<"3. ="<<n3<<endl;
 
ask:
 cout<<"Enter Your Choice Here: ";
 cin>>choice;
 if (choice == 1)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n1<<endl;
 one=one+3;//nom1 gets 3 points
 }
 else if (choice == 2)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n2<<endl;
 two=two+3;//nom2 gets 3 points
 }
 else if (choice == 3)
 {
 cout<<""<<endl;
 cout<<"You voted "<<n3<<endl;
 three=three+3;//nom3 gets 3 points
 }
 else
 {
 cout<<"You Didn't Enter An Available Option"<<endl;
 goto ask;//goes back to question
 }
 }
}
void voting_system::voting_option_3rdplace(string n1,string n2,string n3,int a,int b,int c)
{
	int vote;
	int choice;
	cout<<"You Are Voting for 2nd Committee member of the club"<<endl;
	cout<<"Would You Like To Vote?"<<endl<<endl;
 cout<<"1=Yes, 2=No : "; 
 cin>>vote;
 if (vote == 1)
 { 
 cout<<"\nThese are the nominees:"<<endl<<endl;
 cout<<"1. ="<<n1<<endl;
 cout<<"2. ="<<n2<<endl;
 cout<<"3. ="<<n3<<endl;
 
ask:
 cout<<"Enter Your Choice Here: ";
 cin>>choice;
 if (choice == 1)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n1<<endl;
 one=one+1;
 }
 else if (choice == 2)
 {
 cout<<""<<endl;
 cout<<"You voted for "<<n2<<endl;
 two=two+1;
 }
 else if (choice == 3)
 {
 cout<<""<<endl;
 cout<<"You voted "<<n3<<endl;
 three=three+1;
 }
 else
 {
 cout<<"You Didn't Enter An Available Option"<<endl;
 goto ask;
 }
 }
}
void voting_system::display_points(string n1,string n2,string n3,int a,int b,int c)
{
	cout<<"Nominee number"<<setw(20)<<"Nominee Name"<<setw(30)<<"Standing(total points)"<<endl;
	cout<<"1"<<setw(32)<<n1<<setw(20)<<a<<endl;
	cout<<"2"<<setw(32)<<n2<<setw(20)<<b<<endl;
	cout<<"3"<<setw(32)<<n3<<setw(20)<<c<<endl;
	cout<<"4"<<setw(32)<<"nominee no."<<setw(20)<<"points"<<endl;
}
void Mem_Particulars::setID_Name_ad_hp(string id,string n,string ad,string hp)
{
	cout<<"(Voter)Enter your student admin no. :";
	getline(cin,id);
	cout<<"(Voter)Enter your name :";
	getline(cin,n);
	cout<<"(Voter)Enter your address :";
	getline(cin,ad);
	cout<<"(Voter)Enter your handphone number :";
	getline(cin,hp);
	idNum=id;
	name=n;
	address=ad;
	hpno=hp;
	fflush(stdin);
}
void Mem_Particulars::showData()
{
	cout<<"\nStudent ID= :"<<idNum;
	cout<<"\nStudent Name= :"<<name;
	cout<<"\nStudent Address = :"<<address;
	cout<<"\nStudent Handphone no.= :"<<hpno;
    cout<<endl<<endl;
}
void Mem_Particulars::write_in_data()
{
	ofstream Name;

	Name.open("d:\\Mem_particulars.txt",ios::app);
	Name<<idNum<<"\n"<<name<<"\n"<<address<<"\n"<<hpno<<endl;
	Name.close();
}
void Nom_Particulars::nom1_setID_Name_ad_hp(string id,string name,string ad,string hp)
{
	cout<<"(Nominee)Enter your student admin no. :";
	getline(cin,id);
	cout<<"(Nominee)Enter your name :";
	getline(cin,name);
	cout<<"(Nominee)Enter your address :";
	getline(cin,ad);
	cout<<"(Nominee)Enter your handphone number :";
	getline(cin,hp);
	idNum=id;
	n1=name;
	address=ad;
	hpno=hp;
	fflush(stdin);
}
void Nom_Particulars::nom1_showData()
{
	cout<<"\nNominee ID= :"<<idNum;
	cout<<"\nNominee Name= :"<<n1;
	cout<<"\nNominee Address = :"<<address;
	cout<<"\nNominee Handphone no.= :"<<hpno;
    cout<<endl<<endl;
}
void Nom_Particulars::nom1_write_in_data()
{
	ofstream Name;

	Name.open("d:\\Nom_particulars.txt",ios::app);
	Name<<idNum<<"\n"<<name<<"\n"<<address<<"\n"<<hpno<<endl;
	Name.close();
}
void Nom_Particulars::nom2_setID_Name_ad_hp(string id,string name,string ad,string hp)
{
	cout<<"(Nominee)Enter your student admin no. :";
	getline(cin,id);
	cout<<"(Nominee)Enter your name :";
	getline(cin,name);
	cout<<"(Nominee)Enter your address :";
	getline(cin,ad);
	cout<<"(Nominee)Enter your handphone number :";
	getline(cin,hp);
	idNum=id;
	n2=name;
	address=ad;
	hpno=hp;
	fflush(stdin);
}
void Nom_Particulars::nom2_showData()
{
	cout<<"\nNominee ID= :"<<idNum;
	cout<<"\nNominee Name= :"<<n2;
	cout<<"\nNominee Address = :"<<address;
	cout<<"\nNominee Handphone no.= :"<<hpno;
    cout<<endl<<endl;
}
void Nom_Particulars::nom2_write_in_data()
{
	ofstream Name;

	Name.open("d:\\Nom_particulars.txt",ios::app);
	Name<<idNum<<"\n"<<name<<"\n"<<address<<"\n"<<hpno<<endl;
	Name.close();
}
void Nom_Particulars::nom3_setID_Name_ad_hp(string id,string name,string ad,string hp)
{
	cout<<"(Nominee)Enter your student admin no. :";
	getline(cin,id);
	cout<<"(Nominee)Enter your name :";
	getline(cin,name);
	cout<<"(Nominee)Enter your address :";
	getline(cin,ad);
	cout<<"(Nominee)Enter your handphone number :";
	getline(cin,hp);
	idNum=id;
	n3=name;
	address=ad;
	hpno=hp;
	fflush(stdin);
}
void Nom_Particulars::nom3_showData()
{
	cout<<"\nNominee ID= :"<<idNum;
	cout<<"\nNominee Name= :"<<n3;
	cout<<"\nNominee Address = :"<<address;
	cout<<"\nNominee Handphone no.= :"<<hpno;
    cout<<endl<<endl;
}
void Nom_Particulars::nom3_write_in_data()
{
	ofstream Name;

	Name.open("d:\\Nom_particulars.txt",ios::app);
	Name<<idNum<<"\n"<<name<<"\n"<<address<<"\n"<<hpno<<endl;
	Name.close();
}

This is my main.cpp.
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
#include "Class Definition.h"
void main()
{
	int temp=0;
	char hold = 'y';
	int membercount=0;
	int x=0;
	char y='y';
	char z='y';
	string check;
	Mem_Particulars member;
	cout<<"===============REGISTERING PROCESS================"<<endl;
	while(x!=1)
	{
		cout<<"Would you like to enter a new set of particulars?(y=yes/n=no):"<<endl;
		cin>>y;
		if(y=='y')
		{
			fflush(stdin);
			member.setID_Name_ad_hp(id,n,ad,hp);
			member.showData();
			member.write_in_data();
			membercount++;//This is to see how many members are there
		}
		else
			break;
	}
	
	Nom_Particulars nominee;
	fflush(stdin);
	nominee.nom1_setID_Name_ad_hp(id,n1,ad,hp);
	nominee.nom1_showData();
	nominee.nom1_write_in_data();
    nominee.nom2_setID_Name_ad_hp(id,n2,ad,hp);
	nominee.nom2_showData();
	nominee.nom2_write_in_data();
	nominee.nom3_setID_Name_ad_hp(id,n3,ad,hp);
	nominee.nom3_showData();
	nominee.nom3_write_in_data();
	voting_system votes;
	int i;
	int j;

cout<<"====================VOTING PROCESS===================="<<endl;	
for (i=0,j=1;i<membercount;i++,j++)
{	
	
	cout<<"In order of registered sequence,Member no."<<j<<"'s turn to vote"<<endl;
	votes.voting_option_1stplace(n1,n2,n3, one, two, three);
	votes.voting_option_2ndplace(n1,n2,n3, one, two, three);
	votes.voting_option_3rdplace(n1,n2,n3, one, two, three);
}
cout<<"========================RESULTS======================="<<endl;
   votes.display_points(n1,n2,n3, one, two, three);
   cout<<"Quit?";
   cout<<"Input 'y' if yes :";
   cin>>hold;
   while(temp=0)
   {
	   if(hold=='y')
		   temp=1;
   }
}
Sep 1, 2010 at 7:53am
I only understand that because I have included my header file in both cpp files,it probably means that my variables were defined twice.

Any advice or solution would be greatly apprieciated and please explain them as I am keen to learn.
Sep 1, 2010 at 10:21am
Please read about extern variable , i think that it should be not declared where the variable are declared .but in the different file where the variable is used.
Sep 1, 2010 at 10:23am
also u should only use

#include "Defination.h"

not


#include "Class Defination.h"
Sep 1, 2010 at 12:54pm
In your header file change this:
1
2
3
	extern int one=0;
	extern int two=0;
	extern int three=0;

to this
1
2
3
	extern int one;
	extern int two;
	extern int three;

And in your cpp file add this:
1
2
3
4
5
6
7
8
9
10
	int one=0;
	int two=0;
	int three=0;
	string n1;
	string n2;
	string n3;
	string id;
	string n;
	string ad;
	string hp;

You have correctly declared your globals in the header file using extern. But you should not initialise them when you declare them. Also what you didn't do is actually define the globals that you declared. That is why they need to appear in the cpp file without the keyword 'extern'.

Basically a global should be defined exactly once in the program in a cpp file. But they must be declared everywhere they will be used in a header file using the 'extern' keyword.
Sep 1, 2010 at 5:22pm
Much thanks,it solved the problems I had,and I think iv got the hang of it now.
However a new error occured,
/Error 7 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup MSVCRTD.lib Project with header/.
Maybe I'm just blind, as I looked it up and followed some suggestions to go to linker under properties and do something to it,but I can't find linker anywhere.
I'm using windows visual studios 2008.

Thanks, I resolved my linker issue as well.
Last edited on Sep 1, 2010 at 5:33pm
Sep 1, 2010 at 5:34pm
I'm afraid I can't help you there. I haven't used Windows for many years.
Sep 1, 2010 at 6:09pm
It's also worth noting that tons of global variables (as well as variables named "one", "two", "three", and similarly named variables like "n1", "n2", "n3") are all horribly atrocious things that you should not be doing in your program.

I would recommend a redesign.
Last edited on Sep 1, 2010 at 6:10pm
Topic archived. No new replies allowed.