2012-03-10 17 views
0

これで、単純なゲームを試してみました。私は、各部品すなわちヘルメット、ボディなどのために内部に構造体を持つEquipmentという構造体を持っています。Equipmentのコンストラクタでは、サブ構造体のオブジェクトを作成し、サブ構造体のコンストラクタで文字列ベクトルを初期化します。ネストした構造体とC++でデータにアクセスする

私の主な機能では、私は機器オブジェクトを作成しますが、woodHelmなどをアクセスしようとすると、コンパイルエラーが発生します。 'struct equipment'には 'helm'という名前のメンバーはありません。私は間違って何をしていますか?それとも私はそれをもっとうまくやれるか?

#ifndef EQUIP_H 
#define EQUIP_H 
#include <vector> 
#include <string> 

using namespace std; 

struct Equipment{ 
    Equipment(); 
    struct Helmet{ 
    Helmet(){ 
     const char* tmp[] = {"Wooden Helmet","1",""}; 
     vector<string> woodHelm (tmp,tmp+3); 
     const char* tmp1[] = {"Iron Helmet","2",""}; 
     vector<string> ironHelm (tmp1,tmp1+3); 
     const char* tmp2[] = {"Steel Helmet","3",""}; 
     vector<string> steelHelm (tmp2,tmp2+3); 
     const char* tmp3[] = {"Riginium Helmet","5","str"}; 
     vector<string> rigHelm (tmp3,tmp3+3); 
    } 
    }; 
    struct Shield{ 
    Shield(){ 
     vector<string> woodShield(); 
     vector<string> ironShield(); 
     vector<string> steelShield(); 
     vector<string> rigShield(); 
    } 
    }; 
    struct Wep{ 
    Wep(){ 
     vector<string> woodSword(); 
     vector<string> ironSword(); 
     vector<string> steelSword(); 
     vector<string> rigSword(); 
    } 
    }; 
    struct Body{ 
    Body(){ 
     vector<string> woodBody(); 
     vector<string> ironBody(); 
     vector<string> steelBody(); 
     vector<string> rigBody(); 
    } 
    }; 
    struct Legs{ 
    Legs(){ 
     vector<string> woodLegs(); 
     vector<string> ironLegs(); 
     vector<string> steelLegs(); 
     vector<string> rigLegs(); 
    } 
    }; 
    struct Feet{ 
    Feet(){ 
     vector<string> leatherBoots(); 
     vector<string> ironBoots(); 
     vector<string> steelBoots(); 
     vector<string> steelToeBoots(); 
     vector<string> rigBoots(); 
    } 
    }; 
}; 


#endif 

Equipment.cpp:

#include <iostream> 
#include "Equipment.h" 

using namespace std; 

Equipment::Equipment(){ 
    Helmet helm; 
    Shield shield; 
    Wep wep; 
    Body body; 
    Legs legs; 
    Feet feet; 
} 

とmain.cppに:

ここ

は私Equipment.h(私はまだそれらを初期化しhaventは、他のサブ構造体を無視する)であります

#include <iostream> 
#include "Player.h" 
#include "Equipment.h" 
#include "Items.h" 
#include "conio.h" 

using namespace std; 
using namespace conio; 

void init(); 

int main(int argc, char* argv[]){ 
    /****INIT****/ 
    Player p(argv[1],10,1,1); 
    Equipment equip; 
    Items items; 

    cout << clrscr() << gotoRowCol(3,5) << "Player Stats: (" << p.getName() << ")"; 
    cout << gotoRowCol(4,5) << "HP: " << p.getHP() << 
    gotoRowCol(5,5) << "Att: " << p.getAtt() << 
    gotoRowCol(6,5) << "Def: " << p.getDef(); 

    //this is where it is messing up 
    p.addHelm(equip.helm.woodHelm); 


    cout << gotoRowCol(20,1); 
} 

答えて

4

このような構造体を入れ子にすることはお勧めできません。それぞれを親レベルに置くことです。なぜ "クラス"を使用しないのですか?しかし、あなたがやっていることは可能です。次のコードビットは正しい方向にあなたを設定するかもしれません:

#include <iostream> 

struct A { 
    struct B { 
     int c; 
     B() { 
      c = 1; 
     } 
    }; 

    B b; 
}; 


int main() { 
    A a; 

    std::cout << a.b.c; 
} 
+0

はありがとうのようなものを追加することでした。それは理にかなっている。それは私が好きだったより少しコードですが、それは動作します。 – adamk33n3r

1

あなたの機器の構造体の名前ヘルムのメンバーを持っていないので。あなたはあなたのコンストラクタにローカル変数helmを持っていますが、コンストラクタが終了するとただちにそれが存在しなくなります。あなたはおそらく望ん

この

struct Equipment { 
    struct Helmet { 
     ... 
    }; 
    Helmet helm; /// this is the line you are missing 
    struct Shield { 
     ... 
    }; 
    ... 
}; 
+0

私が使用した構造体名を使用して、この例をありがとう。これは、@ジェームソンと一緒になって、私を助けてくれました。 – adamk33n3r

+0

ようこそ。答えがあなたを助けたら、答えの左上にある大きな数字の上にある上向き矢印を使って投票してください。 – ComicSansMS

+0

私はそれをすることができますが、私はそれを行うには15の評判が必要と言う:/ – adamk33n3r

関連する問題