2012-03-10 64 views
0

好吧,我正在做一个简单的游戏的实验。我有一个名为Equipment的结构,其中包含了用于每个部分(即Helmet,Body等)的结构。在Equipment的构造函数中,我使用它来创建子结构的对象,并在子结构构造函数中让它们初始化字符串向量。在C++中嵌套构造和访问数据

所以我的问题是,在我的主要功能中,我创建了一个设备对象,但是当我尝试访问例如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 

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中:

#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 2012-03-10 08:18:18

1

因为您的设备结构中没有名称helm的成员。你只需要在你的构造函数中有一个局部变量helm,但是一旦构造函数退出,它就不再存在了。

你可能想要的是添加这样的

struct Equipment { 
    struct Helmet { 
     ... 
    }; 
    Helmet helm; /// this is the line you are missing 
    struct Shield { 
     ... 
    }; 
    ... 
}; 
+0

感谢您使用我使用的结构名称的例子。这和@Jameson一起给了我很大的帮助。 – adamk33n3r 2012-03-10 08:19:17

+0

不客气。如果答案对您有帮助,请使用答案左上方大数字上方的向上箭头进行投票。 – ComicSansMS 2012-03-10 08:25:04

+0

我希望我可以但它说我需要15声望做到这一点:/ – adamk33n3r 2012-03-10 19:40:56