C++ ~ 在客户端中调用函数会给出错误:"identifier ____ is undefined"
C++ ~ call to function in client gives error: "identifier ____ is undefined"
我将向您介绍一个涉及多个不同文件的问题。我不知道为什么会出现标题中指定的错误。让我把文件放在下面,然后从那里开始。
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];
}
我不想被人用勺子舀,也不想有人帮我解决问题,我正在努力弄清楚我做错了什么,这样我的项目的剩余部分就不会像一直以来那样坎坷。
回顾我的错误,"identifier"Populate Zombies"是未定义的",我无法想象为什么会这样。这可能与我做事的范围有关吗?如果我遗漏了任何代码(我没有把所有的东西都放进去,但我认为我有所有相关的东西),只要告诉我,我就可以来回交谈。
提前感谢所有试图提供帮助的人:)
-Anthony
通常,您使用变量来调用函数,而不是在类中直接调用它:
ZombieListClass zombieList=new ZombieListClass(); // add a variable here
ZombieRec zombieList[MAX_ZOMBIES];
zombieList.PopulateZombies(zombieList[MAX_ZOMBIES]); // See the difference?
我不确定您发布的错误是否是唯一的错误。这是我在你的主.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
}
我的建议是,在着手解决这样的问题之前,重新审视构造函数和类定义的概念。
相关文章:
- 我看到"use of undeclared identifier"错误,有人可以告诉我如何解决它吗?
- 消除"Undeclared identifier"错误
- 为什么我在" 'X' identifier not found' "上收到错误
- C++:将运算符作为参数传递会导致错误"expected an identifier"
- 当我尝试在代码中使用宏时,我收到类似"expected identifier"的错误
- 什么是'undeclared identifier'错误,如何解决?
- 使用 ios::Nocreate 标志会导致"undeclared identifier"错误
- 如何在Xcode中将长双精度转换为字符串格式?或如何解决错误"use of undeclared identifier 'to_string' "
- C++ 成员函数错误 在另一个成员函数中使用时"Undeclared Identifier"
- "Undeclared identifier"错误,如果尝试在 C++ 中的 Windows 下包含 libvlc
- 错误 "identifier not declared in this scope" - C++
- 前向声明不够时如何避免"'identifier' uses undefined class/struct/union 'name'"错误?
- 在 C 枚举中使用单词 "SING" 错误以"expected an identifier"
- C++ ~ 在客户端中调用函数会给出错误:"identifier ____ is undefined"
- Unreal Engine 4.19 C++Undeclared Identifier错误,但它的代码是IS声明的
- C++函数和'identifier not found'错误
- C++ 绕道 3.0 快递在 MVS 2012 错误"identifier not found"
- 如何解决"undeclared identifier"错误?
- 创建 MB 库,错误"expected an identifier"
- 访问子类中的"protected"数据时"Identifier is undefined"错误