좋아, 그럼 간단한 게임을 만들면서 실험하고 있습니다. 나는 구조체와 함께 각 부품 즉 헬멧, 몸체 등을 위해 구조체가있는 구조체를 가지고 있습니다. 장비의 생성자에서 하위 구조체의 객체를 만들고 서브 구조체 생성자에서 문자열 벡터를 초기화해야합니다.중첩 된 구조체 및 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);
}
감사합니다 같은 것을 추가했다. 그건 의미가 있습니다. 그것은 내가 좋아했을 것보다 조금 더 많은 코드이지만 작동합니다. – adamk33n3r