我带着一个涉及多个不同文件的问题来找你。我不确定为什么我会收到标题中指定的错误。让我把文件放在下面并从那里开始。
DummyClient.cpp
#include "Gameboard.h" //for Gameboard
#include "Location.h" //for function prototypes
#include "zList.h" //for Zombies
#include <iostream> //for input/output stream
using namespace std;
void main()
{
srand(123456789);
Gameboard myGB;
myGB = Gameboard();
ZombieListClass();
ZombieRec zombieList[MAX_ZOMBIES];
PopulateZombies(zombieList[MAX_ZOMBIES]); // this throws the error here of "Error: identifier "PopulateZombies" is undefined"
}
zList.h
#ifndef ZLIST_H
#define ZLIST_H
#include "Location.h" // for list record
#include "ZombieRec.h"
#include "Gameboard.h"
class ZombieListClass
{
public:
ZombieListClass(); //default constructor
void PopulateZombies(ZombieRec zombieList[]);
bool IsInBounds(int row, int col);
private:
ZombieRec list[MAX_ZOMBIES]; //stores the items in the list
int length; //# of values currently in the list
int currPos; //position of current element
int strength; // health and attack units of a zombie
};
#endif
zList.cpp
#include "zList.h"
ZombieListClass::ZombieListClass() //default constructor
{
length = 0;
currPos = 0;
strength = 5;
LocationRec zombieLoc;
}
void ZombieListClass::PopulateZombies(ZombieRec zombieList[])
{
int row, col;
for (int i = 0; i < MAX_ZOMBIES; i++)
{
row = rand() % MAX_ROW + 1;
col = rand() % MAX_COL + 1;
while (!IsInBounds(row, col))
{
row = rand() % MAX_ROW + 1;
col = rand() % MAX_COL + 1;
}
zombieList[i].currLoc.row = row;
zombieList[i].currLoc.col = col;
}
}
bool ZombieListClass::IsInBounds(int row, int col)
{
if (row == 0 || row == MAX_ROW + 1 || col == 0 || col == MAX_COL + 1)
{
return false;
}
else
{
return true;
}
}
Gameboard.h
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include "Location.h"
#include "ZombieRec.h"
#include "zList.h"
const int MAX_ROW = 3; // total number of rows in the board
const int MAX_COL = 3; // total number of cols in the board
class Gameboard
{
public:
Gameboard();
private:
int boardSizeArr[MAX_ROW + 2][MAX_COL + 2];
}; // end Gameboard
#endif
最后,Gameboard.cpp
#include "Gameboard.h"
Gameboard::Gameboard()
{
// Declares a board with a boundary along the outside
boardSizeArr[MAX_ROW + 2][MAX_COL + 2];
}
我不希望被人掏空,而且有人为我解决我的问题,我想弄清楚我做错了什么,以便我项目的剩余部分不是&#39 ; t像这一直一样颠簸。
回顾我的错误,&#34;识别&#34; PopulateZombies&#34;未定义&#34;,我无法想象为什么会这样。这可能与我做事的范围有关吗?如果我已经删除了任何代码(我没有把所有内容都放在那里,但我认为我有相关的一切)请告诉我,我能够来回交谈,只要这需要
感谢所有提前帮助的人:)
- 安东尼
答案 0 :(得分:1)
通常,您使用变量调用函数,而不是在类中定义时直接调用它:
ZombieListClass zombieList=new ZombieListClass(); // add a variable here
ZombieRec zombieList[MAX_ZOMBIES];
zombieList.PopulateZombies(zombieList[MAX_ZOMBIES]); // See the difference?
答案 1 :(得分:0)
我不确定您发布的错误是否是唯一的错误。这是我在main.cpp中看到的内容
#include "Gameboard.h" //for Gameboard
#include "Location.h" //for function prototypes
#include "zList.h" //for Zombies
#include <iostream> //for input/output stream
using namespace std;
void main()
{
srand(123456789);
Gameboard myGB;
myGB = Gameboard();//The constructor"Gameboard()" is automatically called when you defined
//myGB in the previous line,
ZombieListClass();//see Hai Bi's great answer on this one
ZombieRec zombieList[MAX_ZOMBIES];//ZombieRec is a member of ZombieListClass, use . to access it
PopulateZombies(zombieList[MAX_ZOMBIES]); //Also see Hai Bi's answer
}
我的建议是重新审视constructor and class definition的概念,然后再提出这样的问题。