有关如何解决此错误的建议。 [英] Advice on how to resolve this error.

查看:170
本文介绍了有关如何解决此错误的建议。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C ++的新手,我正在构建一个模拟兔子群的程序。我有点卡住如何解决这个问题,如何让方法来识别我的全局指针变量。我尝试编译我的程序时遇到此错误。

 在此处输入代码
main.cpp:在函数'bool ageCheck(BunnyNode *)'中:
main.cpp:133:5:error:'head'未在此范围内声明
if(head){
^

我还有几个类似这个错误的错误。我的印象是,如果我明白为什么会出现这种错误,我将能够理清其他问题。我从ageCheck()方法中选择了一个错误,该方法应该遍历兔子的链接列表并检查它们的年龄。
这就是我所拥有的

 在这里输入代码
#include< iostream>
#include< string>
#include< vector>
#include< cstdlib>

//#includelistofbunny.h

使用std :: cin;
使用namespace std;

typedef struct BunnyNode {
string * name;
int age;
bool性别;
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();
字符串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;
}
}


解决方案

A典型的链表结构由三部分组成

数据

  class Bunny 
{
string name; //除非你真的需要它们,否则不要使用指针
int age;
bool性别;
字符串颜色;
bool radioactive_bunny;
public:
字符串getGender(); //不需要知道哪个Bunny,因为
//这些函数绑定到一个特定的Bunny
字符串getName();
...
};

节点

  struct Node 
{
Bunny数据; //我们现在会忽略模板。但他们是rilly kool!
Node * next; //这里是使用指针的好时机:指向下一个节点
Node():next(nullptr)//节点构造函数。这真的有帮助。相信我。
{
}
}

节点除了数据和指向下一个 Node 的链接外什么也不知道。您可以创建一个 Node 的笨蛋,您就越安全。另请注意, Node 包含数据。这使您可以轻松地交换数据,而无需重新编写整个节点,稍后将您设置为轻松地对列表结构进行模板化(尽管您可能更适合跳转到 std ::列表)。



和链接列表:

  class LinkedList 
{
Node * head;
Node * tail;
节点*当前; //不如您想象的那样有用
public:
LinkedList():head(nullptr),tail(nullptr),current(nullptr)
{
}
void add(兔子&兔子);
void remove(const string& bunnyname);
Bunny& get(const string& bunnyname);
Bunny& GETNEXT(); //当前可能对此有所帮助,但请注意迭代器
...
};

请注意,我们绝不会让调用者在 Node 。他们可以做一些像 delete 或者mangle Node:next 之类的愚蠢行为。



在堆栈溢出中添加,删除和遍历列表已被击败致死,所以您应该能够找到大量的例子来说明如何做到这一点。例如:使用指针从单链表中删除项目。在我看来,在这个环节中非常重要的一个技巧非常值得花时间学习。指针就像火:一个方便的仆人,但一个可怕的主人。

获取链表的重要技巧是使用铅笔和纸来绘制列表和节点。看看他们是如何连接的。当您添加,删除等等时,一步一步地重新列出列表...以便您可以看到它是如何完成的。然后编写代码以匹配图纸。我知道。说起来容易做起来比做起来容易,但比没有任何计划将你的头撞在墙上更容易。


Hello I am new fairly newly to C++ and I am constructing a program that will simulate a colony of bunnies. I am a bit stuck on how to resolve this issue on how to get methods to recognize my global pointer variables. I get this error when I try to compile my program.

enter code here
main.cpp: In function ‘bool ageCheck(BunnyNode*)’:
main.cpp:133:5: error: ‘head’ was not declared in this scope
if(head){
   ^

I have several more errors that are similar to this one. I am under the impression that if I understand why this error is being given, I will be able to sort out the others. I chose an error from the ageCheck() method that is supposed to traverse the linked list of bunnies and check their ages. This is what I have

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;
    }
}

解决方案

A typical linked list structure is made of three parts

The Data

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();
    ...
};

The Node

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.
    {
    }
}

Nodes know nothing except their data and a link to the next Node. The dumber you can make a Node, the safer you are. Also note that the Node contains the Data. This allows you to easily swap out the Data without having to re-write the whole node and sets you up for easy templating of the Lined List structure later (though you're probably better off jumping to std::list).

And the Linked 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
    ...
};

Note that we never let the caller at a Node. They could do something stupid like delete it or mangle Node::next.

Adding, removing and iterating through the list has been beaten to death on Stack Overflow, so you should be able to find tonnes of examples of how to do this. For example: Using pointers to remove item from singly-linked list. There is, in my opinion, a really important trick in that link well worth the time spent learning. Pointers are like fire: A handy servant, but a terrifying master.

The big trick to getting linked lists is use a pencil and paper to draw the list and the nodes. See how they are connected. Redraw the list step by step as you add, remove, etc... so you can see how it needs to be done. Then write code to match the drawings. I know. Easier said than done, but way easier than banging your head against a wall with no plan whatsoever.

这篇关于有关如何解决此错误的建议。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆