Estructuras anidadas y acceso a datos en C ++

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.

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?

Here is my Equipment.h (ignore the other sub structs, i havent initialized them yet):

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

using namespace std;

struct Equipment{
    struct 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{
        vector<string> woodShield ();
        vector<string> ironShield ();
        vector<string> steelShield ();
        vector<string> rigShield ();
    struct Wep{
        vector<string> woodSword ();
        vector<string> ironSword ();
        vector<string> steelSword ();
        vector<string> rigSword ();
    struct Body{
        vector<string> woodBody ();
        vector<string> ironBody ();
        vector<string> steelBody ();
        vector<string> rigBody ();
    struct Legs{
        vector<string> woodLegs ();
        vector<string> ironLegs ();
        vector<string> steelLegs ();
        vector<string> rigLegs ();
    struct Feet{
        vector<string> leatherBoots ();
        vector<string> ironBoots ();
        vector<string> steelBoots ();
        vector<string> steelToeBoots ();
        vector<string> rigBoots ();



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

using namespace std;

    Helmet helm;
    Shield shield;
    Wep wep;
    Body body;
    Legs legs;
    Feet feet;

y 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[]){
    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

    cout << gotoRowCol(20,1);

preguntado el 10 de marzo de 12 a las 07:03

2 Respuestas

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;

respondido 10 mar '12, 07:03

Thank you. That makes sense. It is a little more code than i would have liked but it works. - Adán Keenan

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.

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 {

respondido 10 mar '12, 08:03

Thank you for the example with using the struct names i used. This, combined with the @Jameson gave me really helped me out. - Adán Keenan

You're welcome. If the answer helped you, please vote for it using the up arrow above the big number at the top-left of the answer. - ComicSansMS

I wish i could but it says i need 15 reputation to do that :/ - Adán Keenan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.