2016-04-15 14 views
-1

クラス型データメンバで構造体を構築できますか?リンクされたリスト、クラス構造体?

私はクラスファイルを持っています。私の目標は、それぞれ独自の名前と他の属性を持つ文字のリンクされたリントを構築することです。

リストを作成する私はデータとノードポインタを保持するメンバーが必要です 私の質問は私のようなものを構築することができます。これはすべてクラスファイルで行われています。実行するたびに構造体にエラーが発生しますが、BigHeroは型名を指定しません。私は、問題は、それが構造体の定義になったときにC++コンパイラはまだBigHeroクラスを見ていないので、それはで何をすべきかわからないということである疑いがある

ヘルプこのnoobのの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の大きさを知っている必要がありNode、そうNodeを割り当てるために、BigHeroが含まれています。つまり、Nodeを定義する前に、BigHeroを完全に定義し、サイズを知っておく必要があります。

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

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

しかし... Nodeの概念が存在し、Nodeへのポインタが含まれていることができるように他の場所で定義されることを知っている必要がありますBigHero

。これは、前方定義がNodeで解決されています。したがって

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

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

XD uの男...それは働いた!!!!! ...あなたのコンピュータは常にウイルスに感染していない可能性があります – William

+0

@William _ "u the man" _これは男性ユーザーであることをあなたは確信していますか?私は彼らのプロフィールから何の証拠も見ることができません。 –

0

それ。

この置くことによってBigHeroクラスをフォワード-宣言:ノード構造体の前に

class BigHero; 

を。

+1

間違った方法;彼はBigHeroをNodeの値として使用しますが、NodeをBigHeroのポインタとしてのみ使用します。 – kfsone

+0

Nodeが定義される前に彼がBigHeroの前方宣言を必要とするわけではありませんか? – GaiusOctavian

+0

彼は 'node'の定義を' BigHero'の後に移動し、 'BigHero'の前に' Node'を前方に定義する必要があることを意味します。 – user4581301

関連する問題