你好,我對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;
}
}
沒有'頭',而是'BunnyNode :: head'。如果'ageCheck'是'BunnyNode'的成員函數,那麼你現在可能會搖滾和滾動,但事實並非如此。順便說一句,你不必在C++中做'typedef struct'的東西。 – user4581301
附錄:您似乎正在將鏈接列表與節點混合。這會導致你在將來遇到問題。 – user4581301
@ user4581301所以我可以做'struct BunnyNode'? –