Ada*_*nan 0 c++ struct compiler-errors
好的,我正在尝试制作一个简单的游戏.我有一个名为Equipment的结构,其中包含每个部分的结构,即头盔,主体等.在设备的构造函数中,我让它创建子结构的对象,在子结构构造函数中,我让它们初始化字符串向量.
所以我的问题是在我的main函数中我创建了一个设备对象但是当我尝试访问例如woodHelm时,我得到了编译错误:'struct Equipment'没有名为'helm'的成员.我究竟做错了什么?或者还有另一种方法可以做得更好吗?
这是我的Equipment.h(忽略其他子结构,我还没有初始化它们):
#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
Run Code Online (Sandbox Code Playgroud)
Equipment.cpp:
#include <iostream>
#include "Equipment.h"
using namespace std;
Equipment::Equipment(){
Helmet helm;
Shield shield;
Wep wep;
Body body;
Legs legs;
Feet feet;
}
Run Code Online (Sandbox Code Playgroud)
和main.cpp:
#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);
}
Run Code Online (Sandbox Code Playgroud)
好吧,我不建议像这样嵌套结构 - 将每个结构放在父级别.为什么不使用"课堂"?但是,你正在做的事情是可能的.以下代码可能会让您朝着正确的方向前进:
#include <iostream>
struct A {
struct B {
int c;
B() {
c = 1;
}
};
B b;
};
int main() {
A a;
std::cout << a.b.c;
}
Run Code Online (Sandbox Code Playgroud)