2012-03-10 2 views
0

좋아, 그럼 간단한 게임을 만들면서 실험하고 있습니다. 나는 구조체와 함께 각 부품 즉 헬멧, 몸체 등을 위해 구조체가있는 구조체를 가지고 있습니다. 장비의 생성자에서 하위 구조체의 객체를 만들고 서브 구조체 생성자에서 문자열 벡터를 초기화해야합니다.중첩 된 구조체 및 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