有关如何解决此错误的建议。 你好,我是新来的C++新手,我正在构建一个程序,来模拟一群兔子。如何解决这个问题,如何让方法识别全局指针变量,这让我有些困惑。当我试图编译我的程序时,我得到了这个错误 enter code here main.cpp: In function ‘bool ageCheck(BunnyNode*)’: main.cpp:133:5: error: ‘head’ was not declared in this scope if(head){ ^
我还有几个类似的错误。我的印象是,如果我理解为什么会出现这种错误,我将能够解决其他错误。我从ageCheck()方法中选择了一个错误,该方法应该遍历兔子的链接列表并检查它们的年龄。 这就是我所拥有的有关如何解决此错误的建议。 你好,我是新来的C++新手,我正在构建一个程序,来模拟一群兔子。如何解决这个问题,如何让方法识别全局指针变量,这让我有些困惑。当我试图编译我的程序时,我得到了这个错误 enter code here main.cpp: In function ‘bool ageCheck(BunnyNode*)’: main.cpp:133:5: error: ‘head’ was not declared in this scope if(head){ ^,c++,pointers,global-variables,C++,Pointers,Global Variables,我还有几个类似的错误。我的印象是,如果我理解为什么会出现这种错误,我将能够解决其他错误。我从ageCheck()方法中选择了一个错误,该方法应该遍历兔子的链接列表并检查它们的年龄。 这就是我所拥有的 enter code here #include <iostream> #include <string> #include <vector> #include <cstdlib> //#include "listofbunny.h" using
enter code here
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
//#include "listofbunny.h"
using std::cin;
using namespace std;
typedef struct BunnyNode {
string* name;
int age;
bool gender;
string* color;
bool radioactive_bunny;
BunnyNode *next;
BunnyNode *head;
BunnyNode *tail;
BunnyNode *current;
}
char menu();
int randomGeneration(int x);
void generateFeatures(BunnyNode * newBunny);
void startCheck(int pass);
void sizeCheck(bool& terminate);
bool fatherCheck(BunnyNode * bunny, bool& fatherPresent);
bool motherCheck(BunnyNode * bunny);
bool ageCheck(BunnyNode * bunny);
void addBunnyAge();
void addBabyBunny();
void addBunny();
void addBunny(BunnyNode * mother);
int mutantCount();
void mutantTransform();
void purge();
string getGender(BunnyNode * bunny);
string getName(BunnyNode * bunny);
int getColonySize();
void printColony();
void printFeature(BunnyNode * bunny);
void printSize();
bool ageCheck(BunnyNode * bunny){
if(head){
if(bunny->age >= MAX_AGE && bunny->radioactive_bunny == false){
return 1;
}
else if(bunny->age >= MAX_MUTANT_AGE && bunny->radioactive_bunny){
return 1;
}
else
return 0;
}
}
在此处输入代码
#包括
#包括
#包括
#包括
//#包括“listofbunny.h”
使用std::cin;
使用名称空间std;
类型定义结构BunnyNode{
字符串*名称;
智力年龄;
布尔性别;
字符串*颜色;
布尔兔;
BunnyNode*next;
兔淋巴结*头;
兔淋巴结*尾;
BunnyNode*电流;
}
字符菜单();
int随机生成(intx);
void生成特征(BunnyNode*newBunny);
无效开始检查(内部通行证);
void sizeCheck(bool&terminate);
布尔父亲检查(兔子节点*兔子、布尔和父亲在场);
bool-motherCheck(BunnyNode*bunny);
bool-ageCheck(BunnyNode*bunny);
void addBunnyAge();
void addBabyBunny();
void addBunny();
void addBunny(BunnyNode*母亲);
int mutantCount();
无效突变转移();
无效清除();
字符串getGender(BunnyNode*bunny);
字符串getName(BunnyNode*bunny);
int getColonySize();
void printColony();
无效打印特征(BunnyNode*bunny);
void printSize();
bool ageCheck(BunnyNode*bunny){
若有(总目){
如果(兔子->年龄>=最大年龄和兔子->放射性兔子==假){
返回1;
}
否则,如果(兔子->年龄>=最大变异人年龄和兔子->放射性兔子){
返回1;
}
其他的
返回0;
}
}
典型的链表结构由三部分组成
数据
class Bunny
{
string name; // don't use pointers unless you really, really need them
int age;
bool gender;
string color;
bool radioactive_bunny;
public:
string getGender(); // don't need to know which Bunny anymore because
// these functions are bound to a particular Bunny
string getName();
...
};
节点
struct Node
{
Bunny data; // we will ignore templates for now. But they are rilly kool!
Node * next; // here is a good time to use a pointer: to point to the next node
Node(): next(nullptr) // node constructor. This really helps. Trust me.
{
}
}
节点
s除了数据和到下一个节点的链接外,什么都不知道。你能做的节点越蠢,你就越安全。还要注意,节点
包含数据。这使您可以轻松地交换数据,而无需重新写入整个节点,并为以后方便地对行列表结构进行模板化设置(尽管您最好跳到std::List
)
和链表:
class LinkedList
{
Node *head;
Node *tail;
Node *current; // not as useful as you might think
public:
LinkedList(): head(nullptr),tail(nullptr),current(nullptr)
{
}
void add(Bunny & bunny);
void remove(const string & bunnyname);
Bunny & get(const string & bunnyname);
Bunny & getNext(); // current may help here, but look into iterators
...
};
请注意,我们从不让调用者处于节点
。他们可能会做一些愚蠢的事情,比如删除它或者弄乱节点::下一步
在堆栈溢出时,添加、删除和迭代列表已经被打得死去活来,因此您应该能够找到大量如何执行此操作的示例。例如:。在我看来,这一环节有一个非常重要的技巧,值得花时间学习。指针就像火:一个灵巧的仆人,一个可怕的主人
获取链接列表的最大技巧是使用铅笔和纸绘制列表和节点。看看它们是如何连接的。在添加、删除等过程中逐步重新绘制列表。。。所以你可以看到需要如何做。然后编写代码以匹配图形。我知道。说起来容易做起来难,但比毫无计划地把头撞在墙上要容易得多。没有头
但是邦尼诺德::头
。如果ageCheck
是BunnyNode
的一个成员函数,那么您现在可能正在摇摆,但事实并非如此。顺便说一下,你不必在C++中做<代码> TyBuffSrutt内容:AddieMun:你似乎把链接列表与节点合并了。这将导致将来出现问题。@user4581301,这样我就可以执行struct BunnyNode
?struct BunnyNode{guts of BunnyNode}代码>之后,您可以直接调用它BunnyNode
@user4581301代码>缺失我想我开始理解得更好一些了。我有个问题。其他方法,如ageCheck()
,sizeCheck()
会进入LinkedList类吗?函数的位置取决于函数的功能。类表示某种事物或概念。属于该类的函数应该支持该表示。问问自己,“这个类是否需要这个功能才能变得有用?”如果不需要,它就是使用这个类的函数。如果sizeCheck
在特定大小上查找Bunny
s,则听起来链表不需要该功能。它可以编写为一个函数,调用getNext
,直到列表中不再有Bunny
,同时测试每个Bunny
的大小。