2017-03-18 31 views
-4

你好,我對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; 
    } 
} 
+0

沒有'頭',而是'BunnyNode :: head'。如果'ageCheck'是'BunnyNode'的成員函數,那麼你現在可能會搖滾和滾動,但事實並非如此。順便說一句,你不必在C++中做'typedef struct'的東西。 – user4581301

+0

附錄:您似乎正在將鏈接列表與節點混合。這會導致你在將來遇到問題。 – user4581301

+0

@ user4581301所以我可以做'struct BunnyNode'? –

回答

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

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 or mangle Node::next

在堆棧溢出中添加,刪除和遍歷列表已被擊敗致死,所以你應該能夠找到大量的例子來說明如何做到這一點。例如:Using pointers to remove item from singly-linked list。在我看來,在這個環節中非常重要的一個技巧非常值得花時間學習。指針就像火:一個方便的僕人,但一個可怕的主人。

獲取鏈表的最大竅門是使用鉛筆和紙來繪製列表和節點。看看他們是如何連接的。當您添加,刪除等等時,一步一步地重新列表列表,以便您可以看到它是如何完成的。然後編寫代碼以匹配圖紙。我知道。說起來容易做起來比做起來容易,但比沒有任何計劃地將你的頭撞在牆上更容易。

+0

我覺得我開始明白了一點點。我有個問題。諸如'ageCheck()','sizeCheck()'等其他方法會進入LinkedList類嗎? –

+0

函數的作用取決於函數的功能。類表示一些事物或概念。作爲該類成員的函數應該支持該表示。問問自己,「這個班級是否需要這個能力纔能有用?」如果不是,則它是使用該類的函數。如果'sizeCheck'查找超過特定大小的'Bunny',則聽起來不像鏈表需要這種能力。它可以寫成一個函數,調用'getNext',直到列表中沒有更多的'Bunny',測試每個'Bunny'的大小。 – user4581301