Help with a zombie game

Hey I am having a problem when I am compiling my makefile. This is the error that I am having and cannot see what is wrong with my code. I am using multiple header files

/Project/Project$ make
g++ -static InternalTools.cpp ExternalTools.cpp Main.cpp -o ZombieApocalypse
In file included from InternalTools.h:2:0,
from InternalTools.cpp:1:
InternalData.h:27:60: error: ‘readZombieFile’ was not declared in this scope
In file included from ExternalTools.h:1:0,
from ExternalTools.cpp:1:
InternalData.h:27:60: error: ‘readZombieFile’ was not declared in this scope
In file included from InternalTools.h:2:0,
from Main.cpp:1:
InternalData.h:27:60: error: ‘readZombieFile’ was not declared in this scope
make: *** [ZombieApocalypse] Error 1


Hear is all my code

HEADER FILE InternalData.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
#ifndef H_GUARD
#define H_GUARD
#include <string>
using namespace std;


const int size = 100;
const int pathSize = 12;

struct Zombie
{
	int health;
	int distance;
};

struct Weapon
{
	string name;
	int damage;
	int ammunition; 
};

char path[pathSize] = "Zombies.dat";
Zombie zombies[size];
int zombieCount;

bool success = readZombieFile(path,zombies,size,zombieCount);


#endif 


HEADER FILE InternalTools.h
1
2
3
4
5
6
7
#include <string>
#include "InternalData.h"

using namespace std;

// Task 2
bool readZombieFile(char path[], Zombie zombies[], int maxCount, int &zombieCount);


FILE InternalTools.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
#include "InternalTools.h"
#include<fstream>

// Add your functions and implementations here
bool readZombieFile(char path[], Zombie zombies [], int maxCount, int zombieCount)
{
	bool fileSuccess;
	zombieCount = 0;
	fstream dataFile;
	dataFile.open("Zombies.dat", ios::in);
	if(dataFile.fail())
		fileSuccess = false;
	else
		fileSuccess = true;

	if(dataFile)
	while(!dataFile.eof())
	{
		/*getline(dataFile, maxCount);
		getline(dataFile, zombies[zombieCount].health,'\n');
		getline(dataFile, zombies[zombieCount].distance,'\n');
		*/
		dataFile >> maxCount;
		dataFile >>  zombies[zombieCount].health;
		dataFile >>  zombies[zombieCount].distance;
		++zombieCount;
	}
	dataFile.close();

	if(zombieCount > maxCount)
	{
		fileSuccess = false;
	}

	return fileSuccess;
}


Hear is my makefile to compile the program on linux

ZombieApocalypse: InternalTools.cpp ExternalTools.cpp Main.cpp
g++ -static InternalTools.cpp ExternalTools.cpp Main.cpp -o ZombieApocalypse


Hear is the file zombie.dat
2
100
10
150
13

Line 1- Holds the maximum amount of zombies
Line 2- Stores health of zombie
Line 3 - Stores the distance to the zombie and player
Line 4&5 - Repeat of line 2&3

PLEASE HELP ME GET THIS PROGRAM WORKING
Last edited on
As it says InternalData.h does not know about existence of function readZombieFile(); because it wasn't declared in visible scope.
Last edited on
Yes but when I add a function prototype in InternalData.h, the error will state that there are duplicate declarations. The function prototype is in the header file InternalTools.h. I still do not see what is wrong
Move declaration from InternalTools to InternalData
Then it gives me triple the amount of errors that I received before.
Mind posting errors here? And where did you place declaration?
Actually Im not suppose to edit InternalTools.h because we were told not to. I think the problem is in InternalData.h, InternalTools.cpp or my makefile
Then you need to include internalTools into InternalData. And delete include from InternalTools.
Or actually move actual reading in main(). Because it can fail miserably in runtime.
Thats exactly what I did when I started off and when I compiled, there was an infinite loop of errors. So what I did was include this line of code in InternalData.h

1
2
#ifndef H_GUARD
#define H_GUARD 


This is to guard it against multiple
inclusions of this header file.

I dont understand why im getting the error. And I cant carry on with my program because of this error
You forgot to
And delete include from InternalTools.

You have a problem: InternalTools includes InternalData but function from internal data tries to access function declared in IntenalTools.
To put it simply, you cannot make your program to work until you change program architecture completely.
We had instructions NOT to change any code in the header InternalTools.h. I created everything everything else. Something has to be wrong in my header file or .cpp.
Here is the full InternalTools.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
#include <string>
#include "InternalData.h"

using namespace std;

// Task 2
bool readZombieFile(char path[], Zombie zombies[], int maxCount, int &zombieCount);

// Task 3
bool readWeaponFile(char path[], Weapon weapons[], int maxCount, int &weaponCount);

// Task 4
int generateRandom(int from, int to);

// Task 5
int generateDistance(int health, int maximum);

// Task 6
int generateAmmunition(int damage);

// Task 7
void useWeapon(Weapon &weapon, Zombie &zombie, bool &dead, bool &noAmmo);

// Task 8
void inputInteger(string message, int &value, bool &success);

// Task 9
int inputInteger(string message, int maximum);

// Task 10
void inputCharacters(string message, char input[], int maximumLength);

// Task 11
void displayBattle(Zombie &zombie, int maximumDistance);

// Task 12
void displayInfo(Zombie &zombie, Weapon weapons[], int weaponCount);

// Game System
void play();


I still have alot to do and im only stuck on task2
Move lines 23-27 from InternalData to your main() function. Do not place function calls in global namespace. It usually leads to static order initialization fiasco.
Damn you are doing your best to try and help me and all your advice seems valid by we are told to do it differently. We are told to have lines 23-27 specifically in InternalData.h. We are not suppose to work in main just yet.
It is just cannot work that way without some advanced trickery.
Well is there any thing I can change/remove/add in InternalData.h and InternalTools.cpp. Wecan change alot in them if there is a way to fix my problem. There has to be a way
Move lines 23-27 from InternalData.h to some function (like init()) in InternalData.cpp and include InternalTools.h in said cpp file.
We are told to have those lines in InternalData.h, not anywhere else
Here is the actual question
2.2 Task 2 - Reading Zombies from File

Implement a function for populating an array of zombies. Data to populate the array has
to read from a le. The function prototype is given in InternalTools.h and should be
implemented in InternalTools.cpp.

bool readZombieFi le ( char path [ ] , Zombie zombies [ ] , int maxCount ,
int &zombieCount ) ;


The rst parameter (path) is a character array holding the path of the le to open.
The second parameter (zombies) is an empty array that can hold zombies. The third
parameter (maxCount) is the size of the zombie array, hence the maximum number of
zombies the second parameter can hold. The last parameter (zombieCount) are the
number of zombies that were read from le. Since it is a reference, you can (and must)
change the value once the le was read. The function returns a boolean that indicates if
the le was read successfully. Successful" in this case means that the le exists, was not
empty, holds an integer on its rst line and does not contain more zombies than the array
can hold.
A zombie le is structured as follows:
1. The rst line holds the number of zombies in the le. You may assume that this value
is correct. & i.e. the number of complete zombie entries in the le will correspond
to this value.
2. The second line holds the health of the rst zombie.
3. The third line holds the distance between the zombie and the player.
4. The remaining lines are the same as 2. and 3. & i.e. they contain respectively the
health and distance of each remaining zombie, the number of such entries being
determined by the entry specied in 1.
The function you are to implement should create a new Zombie object for each line-pair
in the le, add it to the array (second parameter) and update the last parameter with
the total number of zombies created. If the number of zombies in the le is greater than
the size of the zombie array (in other words, greater than maxCount), the array should be
lled to its capacity and the function should return false.
The function that calls this function can use its return value to observe errors. It can
distinguish between le-related errors and the case of too many zombies by also using the
number of zombies in the array. If no zombies were loaded, the error was le-related. The
function itself should not contain any output statements (print text).
The following code snippet shows how the function might be used. A xed le path
length and zombie array size was used. To conform to the coding standards, you should
add global constants to hold these values to avoid magic numbers. These values should
be set in InternalData.h.

char path [ 1 2 ] = "Zombies.dat" ;
Zombie zombies [ 1 0 0 ] ;
int zombieCount ;
bool s u c c e s s = readZombieFi le ( path , zombies , 100 , zombieCount ) ;
Topic archived. No new replies allowed.