嵌套结构和在 C++ 中访问数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9644836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nested Structs and accessing data in C++
提问by Adam Keenan
Ok so I am experimenting with making a simple game. I have a struct called Equipment with structs inside it for each part i.e. Helmet, Body, etc. In the Equipment's constructor I have it make objects of the sub structs and in the sub structs constructors i have them initialize string vectors.
好的,所以我正在尝试制作一个简单的游戏。我有一个名为 Equipment 的结构,其中每个部分都有结构,即头盔、身体等。在 Equipment 的构造函数中,我让它创建子结构的对象,而在子结构构造函数中,我让它们初始化字符串向量。
So My problem is that in my main function I create an equipment object but when I try to access for example the woodHelm, I get the compile error: ‘struct Equipment' has no member named ‘helm'. What am I doing wrong? Or is there another way I can do it better?
所以我的问题是,在我的主函数中,我创建了一个设备对象,但是当我尝试访问例如 woodHelm 时,出现编译错误:“struct Equipment”没有名为“helm”的成员。我究竟做错了什么?或者还有其他方法可以做得更好吗?
Here is my Equipment.h (ignore the other sub structs, i havent initialized them yet):
这是我的 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
Equipment.cpp:
设备.cpp:
#include <iostream>
#include "Equipment.h"
using namespace std;
Equipment::Equipment(){
Helmet helm;
Shield shield;
Wep wep;
Body body;
Legs legs;
Feet feet;
}
and main.cpp:
和 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);
}
回答by Jameson
Well, I would not recommend nesting structs like this -- put each at parent level. And why not use "class"? But, what you're doing is possible. The following bit of code might set you in the right direction:
好吧,我不建议像这样嵌套结构——将每个结构放在父级。为什么不使用“类”?但是,你正在做的事情是可能的。以下代码可能会让您朝着正确的方向前进:
#include <iostream>
struct A {
struct B {
int c;
B() {
c = 1;
}
};
B b;
};
int main() {
A a;
std::cout << a.b.c;
}
回答by ComicSansMS
Because you don't have a member of name helm in your Equipment struct. You just have a local variable helm in your constructor, but that one ceases to exist as soon as the constructor exits.
因为您的设备结构中没有名称 helm 的成员。您的构造函数中只有一个局部变量 helm,但是一旦构造函数退出,该变量就不再存在。
What you probably wanted was to add something like this
你可能想要的是添加这样的东西
struct Equipment {
struct Helmet {
...
};
Helmet helm; /// this is the line you are missing
struct Shield {
...
};
...
};