why are global variables in header not visible to .cpp file?

source code:
.H:
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
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#ifndef CL_H
#define CL_H
class ALLEGRO_BITMAP;
class collarea
{
public:
	collarea(float x,float y,float w,float h);
	//functions:
	void turn(float n);
	void settheta(float n);
	void setpt(float x, float y);
	void sethitboundaries(ALLEGRO_BITMAP*& a);
	pt getbl();
	pt getbr();
	pt gettl();
	pt gettr();
	pt rotate(pt a);
	pt toa(pt a);
	pt tor(pt a);
	pt abspt(pt a);
	pt geteq(pt a, pt b);
	void gettrig(pt& a);
	int didhit(collarea a);
private:
	float x,y,dx,dy;
	float width,height;
	float theta;
	struct pt {
		float x,y;
		float hyp,theta;
		int set;
	} bl,br,tl,tr;
	
};
#endif 


.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
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
#include "collarea.h"
#include <math.h>
collarea::collarea(float xx,float yy,float w,float h)
{
	x=xx;
	y=yy;
	width=w;
	height=h;
	dx=x/2;
	dy=y/2;
	theta = 0;
}
void sethitboundaries(ALLEGRO_BITMAP*& a)
{
	al_set_target_bitmap(a);
	float r,g,b;
	float mr,mg,mb;
	mr = mb = 255.0;
	mg = 0;
	float lim = (width>height) ? width : height;
	lim = lim/2;
	for(float i=0;i<lim;i++)
	{
		float left = i;
		float right = width-i;
		float up = i;
		float down = height-i;
		//top left
		if(!tl.set)
		{
			al_unmap_rgb_f(al_get_pixel(a,left,up),&r,&g,&b);
			if((r!=mr)||(g!=mg)||(b!=mb))
			{
				tl.x = left;
				tl.y = up;
				tl.set = 1;
				gettrig(tl);
			}
		}
		//top right
		if(!tr.set)
		{
			al_unmap_rgb_f(al_get_pixel(a,right,up),&r,&g,&b);
			if((r!=mr)||(g!=mg)||(b!=mb))
			{
				tl.x = right;
				tl.y = up;
				tl.set = 1;
				gettrig(tr);
			}
		}
		//bottom left
		if(!bl.set)
		{
			al_unmap_rgb_f(al_get_pixel(a,left,down),&r,&g,&b);
			if((r!=mr)||(g!=mg)||(b!=mb))
			{
				tl.x = left;
				tl.y = down;
				tl.set = 1;
				gettrig(bl);
			}
		}
		//bottom right
		if(!br.set)
		{
			al_unmap_rgb_f(al_get_pixel(a,right,down),&r,&g,&b);
			if((r!=mr)||(g!=mg)||(b!=mb))
			{
				tl.x = left;
				tl.y = down;
				tl.set = 1;
				gettrig(br);
			}
		}
	}
	if(!tl.set)
	{
		tl.x=dx;
		tl.y=dy;
		tl.set=1;
		tl.theta=0;
		tl.hyp=0;
	}
	if(!tr.set)
	{
		tr.x=dx;
		tr.y=dy;
		tr.set=1;
		tr.theta=0;
		tr.hyp=0;
	}
	if(!bl.set)
	{
		bl.x=dx;
		bl.y=dy;
		bl.set=1;
		bl.theta=0;
		bl.hyp=0;
	}
	if(!br.set)
	{
		br.x=dx;
		br.y=dy;
		br.set=1;
		br.theta=0;
		br.hyp=0;
	}
}
pt collarea::getbl()
{
	return abspt(rotate(bl));
}
pt collarea::getbr()
{
	return abspt(rotate(br));
}
pt collarea::gettl()
{
	return abspt(rotate(tl));
}
pt collarea::gettr()
{
	return abspt(rotate(tr));
}
pt collarea::toa(pt a) //to adjusted point: origin in middle
{
	pt b;
	b.x = a.x - dx;
	b.y = dy - a.y;
	return b;
}
pt collarea::tor(pt a) //to real point: origin in top left corner
{
	pt b;
	b.x = a.x + dx;
	b.y = dy - a.y;
	return b;
}
pt collarea::abspt(pt a)
{
	pt b;
	b.x = a.x+x;
	b.y = a.y+y;
	return b;
}
int collarea::didhit(collarea a)
{
	int HIT = 0;
	pt spots[8];
	spots[0] = gettl();
	spots[1] = gettr();
	spots[2] = getbr();
	spots[3] = getbl();
	spots[4] = a.gettl();
	spots[5] = a.gettr();
	spots[6] = a.getbr();
	spots[7] = a.getbl();
	float bx,sx,by,sy;
	bx = sx = spots[0].x;
	by = sy = spots[0].y;
	for(int i=0;i<8;i++)
	{
		sx = (spots[i].x < sx) ? spots[i].x : sx;
		bx = (spots[i].x > bx) ? spots[i].x : bx;
		sy = (spots[i].y < sy) ? spots[i].y : sy;
		by = (spots[i].y > by) ? spots[i].y : by;
	}
	float w = bx-sx;
	float h = by-sy;
	pt eqs[8];
	for(int i=0;i<8;i++)
	{
		int ca = ((i+1)%4)+4*(i/4);
		eqs[i] = geteq(spots[i],spots[ca]);
	}
	for(int i=0;i<4;i++)
	{
		int k = (i+1)%4;
		for(int j=4;j<8;j++)
		{
			//if lines aren't parallel (and on top of each other)
			if(eqs[i].x!=eqs[j].x) // i = eq 1 j = eq 2
			{
				//calculate point where they cross
				//x(cross) = (b2 - b1)/(m1 - m2)
				int crx = (eqs[j].y - eqs[i].y)/(eqs[i].x - eqs[j].x);
				if((crx > spots[i].x) && (crx < spots[k].x))
				{
					//crossing point occurs somewhere on the line between points in question
					//its a hit
					HIT = 1;
				}
			}
		}
	}
	return HIT;
}
pt collarea::geteq(pt a, pt b)// x = m (slope) y = b in y = mx + b
{
	//y = mx + b
	pt c;
	c.x = (b.y-a.y)/(b.x-a.x); // m = rise/run
	c.y = a.y - (c.x*a.x); //b = y1 - m*x1
	//double check with b = y2 - m*x2
	float h = b.y - (c.x*b.x);
	if(c.y!=h)
	{
		throw "Formula error geteq(pt a, pt b) in collarea.cpp";
	}
}
void collarea::turn(float n)
{
	theta += n;
	if(theta<0)
	{
		theta += 360;
	}
	else if(theta >= 360)
	{
		theta -= 360;
	}
}
void collarea::settheta(float n)
{
	theta = n;
	if(theta<0)
	{
		theta += 360;
	}
	else if(theta >= 360)
	{
		theta -= 360;
	}
}
void collarea::setpt(float xx,float yy)
{
	x=xx;
	y=yy;
}
void collarea::gettrig(pt& a)
{
	pt b = toa(a);
	a.hyp = sqrt((b.x*b.x)+(b.y*b.y));
	a.theta = atan(b.x/b.y);
}
pt collarea::rotate(pt a)
{
	pt b;
	float ang = a.theta + theta;
	if(ang<0)
	{
		ang += 360;
	}
	else if(ang>360)
	{
		ang -= 360;
	}
	b.y = sin(ang)*a.hyp;
	b.x = cos(ang)*a.hyp;
	return tor(b);
}


error console:

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
collarea.cpp: In function ‘void sethitboundaries(ALLEGRO_BITMAP*&)’:
collarea.cpp:20: error: ‘width’ was not declared in this scope
collarea.cpp:20: error: ‘height’ was not declared in this scope
collarea.cpp:29: error: ‘tl’ was not declared in this scope
collarea.cpp:37: error: ‘gettrig’ was not declared in this scope
collarea.cpp:41: error: ‘tr’ was not declared in this scope
collarea.cpp:46: error: ‘tl’ was not declared in this scope
collarea.cpp:49: error: ‘gettrig’ was not declared in this scope
collarea.cpp:53: error: ‘bl’ was not declared in this scope
collarea.cpp:58: error: ‘tl’ was not declared in this scope
collarea.cpp:61: error: ‘gettrig’ was not declared in this scope
collarea.cpp:65: error: ‘br’ was not declared in this scope
collarea.cpp:70: error: ‘tl’ was not declared in this scope
collarea.cpp:73: error: ‘gettrig’ was not declared in this scope
collarea.cpp:77: error: ‘tl’ was not declared in this scope
collarea.cpp:79: error: ‘dx’ was not declared in this scope
collarea.cpp:80: error: ‘dy’ was not declared in this scope
collarea.cpp:85: error: ‘tr’ was not declared in this scope
collarea.cpp:87: error: ‘dx’ was not declared in this scope
collarea.cpp:88: error: ‘dy’ was not declared in this scope
collarea.cpp:93: error: ‘bl’ was not declared in this scope
collarea.cpp:95: error: ‘dx’ was not declared in this scope
collarea.cpp:96: error: ‘dy’ was not declared in this scope
collarea.cpp:101: error: ‘br’ was not declared in this scope
collarea.cpp:103: error: ‘dx’ was not declared in this scope
collarea.cpp:104: error: ‘dy’ was not declared in this scope
Your terminology is slightly wrong, but did you mean for this line:
void sethitboundaries(ALLEGRO_BITMAP*& a)

To be this:
void collarea::sethitboundaries(ALLEGRO_BITMAP*& a)
yes. wow, that should've been obvious.

What do you mean by 'Your terminology is slightly wrong'?
why are global variables in header not visible to .cpp file?


You weren't having issues with global variables, but class member variables. And don't worry, I've made that mistake a few times. It happens to the best of us.
Topic archived. No new replies allowed.