Header file not being included?

Dec 7, 2011 at 10:17pm
As my project for a c++ course, I'm making a sort of small console game. I am trying to call a function from class Player in Player.h (shown below) in class Itembase contained in Items.h (also below). Items.h does not seem to be recognizing the class Player from the other file and I'm not sure why. In another file in this same project I call another function from class Player and it works just fine. I'm not sure why Items.h doesn't seem to be recognizing the include Player.h.

Items.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
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
#ifndef Items_h
#define Items_h
#include <iostream>
#include <string>
#include "Player.h"
using namespace std;

class ItemBase
{
public:
	ItemBase();
	void equipwep();
	void equiparmor();
	void equipspell();
	void buy();
	void check();
	void equipped();
protected:
	int wa, ma, ad;
	bool isowned;
	string name;
};
ItemBase::ItemBase()
{
	isowned=false;
}


void ItemBase::equipwep()
{
	player.editwepatk(wa);
}
void ItemBase::equiparmor()
{
	player.editarmor(ad);
}
void ItemBase::equipspell()
{
	player.editspell(ma);
}
void ItemBase::equipped()
{
	cout<<name<<"has been equipped\n";
}

void ItemBase::buy()
{
	isowned = true;
}
void ItemBase::check()
{
	static int counter=0;
	counter++;
	if (isowned==true)
	{
		cout<<counter<<"."<<name<<endl;
	}
	else
	{
		cout<<counter<<"."<<endl;
	}
}
#endif 


Dec 7, 2011 at 10:20pm
Player.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#ifndef Player_h
#define Player_h
#include <iostream>
#include "SOTT.h"
#include "AxeofDeath.h"
#include "SteelKatana.h"
#include "Shotgun.h"
#include "WhoopinStick.h"
#include "Switchblade.h"
#include "SOGSY.h"
#include "SRS.h"
#include "IP.h"
#include "SC.h"
#include "SP.h"
#include "Fireball.h"
#include "Acidspit.h"
#include "Maigcpoke.h"
#include "Volcanoattack.h"
#include "WMD.h"
using namespace std;

class Player
{
public:
	Player();
	void NewGame();
	void sally();
	void Shops();
	void bob();
	void merlin();
	void personal();
	void weapons();
	void armor();
	void spells();
	void editwepatk(int);
	void editarmor(int);
	void editspell(int);
protected:
	int strength, agility, magic, resilience,points,wepatk, armordef, magatk, gold;
}player;
Player::Player()
{
	strength=0;
	agility=0;
	magic=0;
	resilience=0;
	points=5;
	gold=500;
}

void Player::NewGame()
{
	char x;
	cout<<"Strength-"<<strength<<endl
		<<"Agility-"<<agility<<endl
		<<"Magic-"<<magic<<endl
		<<"Resilience-"<<resilience<<endl;
	
	cout<<"You have "<<points<<" points left."<<endl
		<<"s for strength\n"
		<<"a for agility\n"
		<<"m for magic\n"
		<<"r for resilience"<<endl;
	x=_getch();// allows user to select where points go
	switch(x)//distributes points
	{
	case 's':
		strength++;
		points--;
		break;
	case 'a':
		agility++;
		points--;
		break;
	case 'm':
		magic++;
		points--;
		break;
	case 'r':
		resilience++;
		points--;
		break;
	default:
		NewGame();
	}
	system("cls");

	if (points!=0)//recalls fuction to distribute the points
		NewGame();
	else 
		Shops();
}


void Player::Shops()
{
	int store;//allows to choose which store
	cout<<"You have "<<gold<<" gold\n";
	cout<<"1.Bruto's Weapons\n"
		<<"2.Bob's Armor\n"
		<<"3.Merlin's Spells\n"
		<<"4.Personal Storage (equip items here)\n";
	store=_getch();
	switch (store)
	{
	case 49:
		{
		system("cls");
		sally();
		break;
		}
	case 50:
		{
		system("cls");
		bob();
		break;
		}
	case 51:
		{
		system("cls");
		merlin();
		break;
		}
	case 52:
		{
		system("cls");
		personal();
		break;
		}
	}
	
}


void Player::sally(void)
{
	int choice;
	cout<<"You have "<<gold<<" gold\n";
	cout<<"Hello, what would you like to purchase?\n"
		<<"1.Axe of Death(2000 gold)\n"
		<<"2.Steel Katana(1000 gold)\n"
		<<"3.Shotgun(500 gold)\n"
		<<"4.The Whoopin Stick(250 gold)\n"
		<<"5.Switchblade(150 gold)\n"
		<<"6.Nevermind\n";
	choice=_getch();
	switch (choice)
	{
	case 49:
		{
			if (gold>=2000)
			{
				aod.buy();
				gold-=2000;
			}
				else
				{
					cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
					_getch();
			}
				break;
		}
	case 50:
		{
			if(gold>=1000)
			{
				sk.buy();
				gold-=1000;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 51://500
		{		
			if (gold>=500)
				{
					shotgun.buy();
					
					gold-=500;
				}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 52://250
		{
			if (gold>=250)
			{
				sw.buy();
				gold-=250;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 53://150
		{
			if(gold>=150)
			{
				sb.buy();
				gold-=150;
			}
			else
			{
				cout<<"Not enough gold\n"
				<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	
	case 58:
		{
			if (gold>=1)
			{
				sott.buy();
				cout<<"\t\tSword of A THOUSAND TRUTHS";
			}
			else 
			{
				cout<<"what the hell?\n";
				_getch();
			}
		}
	}


	system("cls");
	Shops();
}
void Player::bob(void)
{
	int choice;
	cout<<"You have "<<gold<<" gold\n";
	cout<<"Hello, what would you like to purchase?\n"
		<<"1.Steel Plate(2000 gold)\n"
		<<"2.Steel Chainmail(1000 gold)\n"
		<<"3.Iron Plate(500 gold)\n"
		<<"4.Some rusty chainmail(250 gold)\n"
		<<"5.Some old guy to stand in front of you(100 gold)\n"
		<<"6.Nevermind\n";
	choice=_getch();
	switch (choice)
	{
	case (49):
		{
			if (gold>=2000)
			{
				sp.buy();
				gold-=2000;
			}
				else
				{
					cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
					_getch();
			}
				break;
		}
	case 50:
		{
			if(gold>=1000)
			{
				sc.buy();
				gold-=1000;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 51:
		{		
			if (gold>=500)
				{
					ip.buy();
					gold-=500;
				}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 52:
		{
			if (gold>=250)
			{
				srs.buy();
				gold-=250;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 53:
		{
			if(gold>=100)
			{
				sogsy.buy();
				gold-=100;
			}
			else
			{
				cout<<"Not enough gold\n"
				<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	}
	system("cls");
	Shops();
}
void Player::merlin(void)
{
	int choice;
	cout<<"You have "<<gold<<" gold\n";
	cout<<"Hello, what would you like to purchase?\n"
		<<"1.WMD spell(2000 gold)\n"
		<<"2.Volcano attack(1000 gold)\n"
		<<"3.Fireball(500 gold)\n"
		<<"4.Magic Poke(250 gold)\n"
		<<"5.Acid spit(100 gold)\n"
		<<"6.Nevermind\n";
	choice=_getch();
	switch (choice)
	{
	case (49):
		{
			if (gold>=2000)
			{
				wmd.buy();
				gold-=2000;
			}
				else
				{
					cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
					_getch();			
			}
				break;
		}
	case 50:
		{
			if(gold>=1000)
			{
				va.buy();
				gold-=1000;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 51:
		{		
			if (gold>=500)
				{
					fb.buy();
					gold-=500;
				}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 52:
		{
			if (gold>=250)
			{
				mp.buy();
				gold-=250;
			}
			else
			{
				cout<<"Not enough gold\n"<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	case 53:
		{
			if(gold>=100)
			{
				as.buy();
				gold-=100;
			}
			else
			{
				cout<<"Not enough gold\n"
				<<"Press a key to return to shops\n";
				_getch();
			}
			break;
		}
	}
	system("cls");
	Shops();
}
void Player::personal()
{
	char input;
	cout<<"1.Weapons\n"
		<<"2.Armor\n"
		<<"3.Spells\n"
		<<"4.Return to Shops\n";
	input=_getch();
	switch (input)
	{
		case '1':
			{
				weapons();
				break;
			}
		/*case '2':
			{
				armor();
				break;
			}
		case '3':
			{
				spells();
				break;
			}*/
		default:
			{
				Shops();
				break;
			}
	}
			

}
void Player::editwepatk(int wa)
{
	wepatk=wa;
}
void Player::editarmor(int ad)
{
	armordef=ad;
}
void Player::editspell(int ma)
{
	magatk=ma;
}

void Player::weapons()
{
	system("cls");
	cout<<"These are your weapons.\n"
		<<"Press the number on the left of the weapon you wish to equip.\n";
	char input;

	sb.check();
	sw.check();
	shotgun.check();
	sk.check();
	aod.check();
	sott.check();

	input=_getch();
	switch (input)
	{
	case '1':
		{
			sb.equipwep();
			break;
		}
	case '2':
		{
			sw.equipwep();
			break;
		}
	case '3':
		{
			shotgun.equipwep();
			break;
		}
	case '4':
		{
			sk.equipwep();
			break;
		}
	case '5':
		{
			aod.equipwep();
			break;
		}
	case '6':
		{
			sott.equipwep();
			break;
		}
	default:
		{
			weapons();
		}
	}
	cout<<wepatk<<endl;
}

#endif 
Dec 7, 2011 at 10:22pm
I am getting these errors.
Error 2 error C2228: left of '.editwepatk' must have class/struct/union 31
Error 6 error C2228: left of '.editspell' must have class/struct/union 39
Error 4 error C2228: left of '.editarmor' must have class/struct/union 35
Error 1 error C2065: 'player' : undeclared identifier 31
Error 3 error C2065: 'player' : undeclared identifier 35
Error 5 error C2065: 'player' : undeclared identifier 39
all in the Items.h file.

The Player.h file uses several files not include here. They are the classes inheriting class Itembase, but don't seem to be any part of this particular problem.
Dec 7, 2011 at 10:36pm
Is there a reason for not putting the implementation of the methods in .cpp files?

(ie an Items.cpp containing lines 23-62, and a Player.cpp containing lines 41-519)
Last edited on Dec 7, 2011 at 10:38pm
Dec 7, 2011 at 10:46pm
Ya, I started doing the project a couple weeks ago when I just started learning classes and I didn't realize that was the proper way of doing it. It's due in about a week and I haven't had a chance to change that around.
Dec 7, 2011 at 11:38pm
I would recommend that you do split the implementation out of the header files as soon as possible, then fix the problems that arise. You never know, some problems may in fact disappear ;-).

(also, a hint for the declaration of the player global variable - in Player.h, define the class first "class Player { ... };" then declare the global instance of it "extern Player player;", then make sure to define it in the Player.cpp "Player player;" otherwise you'll end up with a linker error).
Last edited on Dec 7, 2011 at 11:50pm
Dec 8, 2011 at 2:32am
Ok, I split the two files including the header files within the .cpp files and put "extern Player player;" within the header then defined "Player player;" in the cpp. I am getting something like 52 errors along the lines of
"Error 34 error LNK2005: "public: void __thiscall Player::weapons(void)" (?weapons@Player@@QAEXXZ) already defined in Items.obj"
and I'm notexactly sure what that means.

I
Last edited on Dec 8, 2011 at 2:35am
Dec 8, 2011 at 2:57am
That means that there is still a duplication of code in the files -- somewhere in Items.cpp (which is what is compiled to make Items.obj) there is an instance of "void Player::weapons(void) { ... }" (perhaps coming from a header file that still has code in it).

Some things to check:

(a) I hope you're not #include'ing the .cpp files anywhere? only header files should be #include'd, because they describe structures and class declarations to be used in the logic;

(b) I hope you've removed the "void Player::weapons(void) { ... }" code from the .h file?

(c) I hope you've done a clean recompile? (ie delete all .obj's before rebuilding, in case you have dependency issues in your makefile/project)

(d) if all of the above checks out okay, then I would suggest either getting your compiler to spit out the result of the preprocessing pass ("g++ -E" if you're using GCC, not sure what the equivalent is for MSVC) and read it through until you find the duplication, alternatively track through the #include's yourself until you find where the duplication is coming from, and remove it.
Last edited on Dec 8, 2011 at 3:00am
Dec 8, 2011 at 3:05am
Thank you so much for all the help. I had accidentily included a the cpp where I thought I had included a header. The program compiles and functions the way I need it to. This was the only major snag I ran into that me and the lab aides could not figure out. Thank you again.
Last edited on Dec 8, 2011 at 3:05am
Topic archived. No new replies allowed.