【问题标题】:Advice on how to resolve this error.有关如何解决此错误的建议。
【发布时间】:2017-08-09 16:32:59
【问题描述】:

您好,我是 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() 方法中选择了一个错误,该方法应该遍历兔子的链表并检查它们的年龄。 这就是我所拥有的

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

【问题讨论】:

  • 没有head,而是BunnyNode::head。如果ageCheckBunnyNode 的成员函数,那么您现在可能正在摇摆不定,但事实并非如此。顺便说一句,你不必在 C++ 中做 typedef struct 的东西。
  • 附录:您似乎将链表与节点混为一谈。这将导致您在未来遇到问题。
  • @user4581301 所以我可以做struct BunnyNode ?
  • struct BunnyNode { guts of BunnyNode }; 之后你可以直接调用它BunnyNode
  • @user4581301 ; 缺失

标签: c++ pointers global-variables


【解决方案1】:

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

数据

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

Nodes 只知道他们的数据和指向下一个Node 的链接。 Node 越笨,你就越安全。另请注意,Node 包含数据。这使您可以轻松交换数据,而无需重新编写整个节点,并为以后轻松模板化内联列表结构做好准备(尽管您最好跳到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
    ...
};

请注意,我们从不让调用者拨打Node。他们可以做一些愚蠢的事情,比如 delete it 或 mangle Node::next

在 Stack Overflow 上添加、删除和迭代列表已经被打死了,所以你应该能够找到大量的例子来说明如何做到这一点。例如:Using pointers to remove item from singly-linked list。在我看来,该链接中有一个非常重要的技巧,非常值得花时间学习。指针就像火:一个得心应手的仆人,但一个可怕的主人。

获取链表的大技巧是使用铅笔和纸来绘制链表和节点。看看它们是如何连接的。在添加、删除等时逐步重绘列表……这样您就可以看到需要如何完成。然后编写代码以匹配图纸。我知道。说起来容易做起来难,但比没有任何计划地把头撞到墙上要容易得多。

【讨论】:

  • 我想我开始理解得更好了。我有个问题。 ageCheck()sizeCheck() 等其他方法会进入 LinkedList 类吗?
  • 函数的去向取决于函数的作用。类代表一些事物或概念。作为该类成员的函数应支持该表示。问问自己,“这个类是否需要这种能力才能发挥作用?”如果不是,它是一个使用该类的函数。如果sizeCheck 查找特定大小的Bunnys,那么听起来链表不需要这种能力。它可以写成一个函数,调用getNext,直到列表中没有更多Bunny,在途中测试每个Bunny的大小。
猜你喜欢
  • 1970-01-01
  • 2013-07-13
  • 2020-12-12
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2017-12-12
相关资源
最近更新 更多