2016-04-15 24 views
-1

你能用類型數據成員構建一個結構嗎?鏈接列表,類結構?

我有一個類文件,我的目標是建立一個鏈接的字符,每個都有自己的名字和其他屬性。

現在建立列表我需要一個成員,將持有的數據和節點指針 我的問題是我可以建立這樣的事情。所有這一切都是在類文件中完成的,每當我運行它時,我都會在結構中出現錯誤,BigHero不會命名類型。

幫助這個菜鳥XD

#include <string> 
#include <iostream> 

using namespace std; 

#ifndef BIGHERO_H 
#define BIGHERO_H 

struct Node{ 
    BigHero data; 
    Node* Next; 
    }; 

class BigHero 
{ 
public: 

    BigHero(); 

    /* Parameterized constructor 
     * Set private members to parameter values 
     * NOTE: level is initialized as the interger division of exp/100 
     *  i.e. if exp = 250 -> level = 2  */ 
     BigHero(string newName, int newExp, int newStr, int newIntel, int   newDex); 

     /* Accessors: each accessor will return the value of the appropriate 
     private data member */ 
     string getName() const; 
     int getExp() const; 
     int getStr() const; 
     int getInt() const; 
     int getDex() const; 
     int getLevel() const; 

     /* Mutators: each mutator will take one parameter and update the  

     appropriate private data member 
     * The domain for each mutator is listed below. 
     * The mutator should protect against invalid values. 
     * An Invalid entry should result in the original value remaining unchanged. */ 
     void setName(string newName); // All strings are valid 
     void setExp(int newExp);  // 0 <= newExp <= 9000 
     void setStr(int newStr);  // 0 <= newStr <= 300 
     void setInt(int newInt);  // 0 <= newInt <= 300 
     void setDex(int newDex);  // 0 <= newDex <= 300 
     void setLevel(int newLevel); // 1 <= newLevel <= 100 


     bool addExp(int amount); 


     void levelUp(); 

     bool operator<(const BigHero& rhs) const; 
     bool operator>(const BigHero& rhs) const; 
     bool operator==(const BigHero& rhs) const; 


     friend ostream& operator<< (ostream& os, const BigHero& rhs); 

    /* Input should be in the format: "Name exp Str Intel Dex" 
    * Don't forget to update level value to be equal to the integer division of exp/100 */ 
     friend istream& operator>> (istream& is, BigHero& rhs); 

    ~BigHero(); 

    private: 
     string name; // Hero's name 
     int exp;  // Experience points (100 exp = 1 level) 
     int level;  // Hero's level 
     int Str;  // Hero's strength 
     int Intel;  // Hero's intelligence 
     int Dex;  // Hero's dexterity 

     Node* head; 
     Node* tail; 


     }; 

     #endif // BIGHERO_H 
+1

#include ? – Pooya

+0

這個結構體是否在類文件 – William

+0

編譯過的類文件? – Pooya

回答

1

Node包含BigHero,所以要分配NodeNode需要知道的BigHero的大小要能確定有多少存儲是必需的。這意味着BigHero必須完全定義,並且它的大小已知,在可以定義Node之前。

class BigHero 
{ 
    // Rest of BigHero omitted to save space 
    Node* head; 
    Node* tail; 
}; 

struct Node{ 
    BigHero data; 
    Node* Next; 
}; 

但是...

BigHero需要知道的Node概念存在,並且會在其他地方規定可以包含指向Node。這是通過Node的正向定義解決的。所以

struct Node; 
class BigHero 
{ 
    // Rest of BigHero omitted to save space 
    Node* head; 
    Node* tail; 
}; 

struct Node{ 
    BigHero data; 
    Node* Next; 
}; 
+0

XD你這個人......它的工作!!!!! ...可能你的電腦總是無病毒 – William

+0

@William _「你是男人」_什麼使你確定這是男性用戶?我無法從他們的個人資料中看到任何證據。 –

0

我懷疑的問題是,C++編譯器還沒有看到尚BigHero類時,得到的結構定義,所以它不知道該怎麼做它。

通過把這個前向聲明BigHero類:

class BigHero; 

節點結構之前。

+1

錯誤的方式;他使用BigHero作爲Node中的值,但只使用Node作爲BigHero中的指針。 – kfsone

+0

這是否意味着在定義Node之前他不需要BigHero的前向聲明? – GaiusOctavian

+0

意味着他需要將'node'的定義移動到'BigHero'之後,並且在'BigHero'前面定義'Node'。 – user4581301