Reputation: 784
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{
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;
}
and 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);
}
Upvotes: 5
Views: 36779
Reputation: 54679
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 {
...
};
...
};
Upvotes: 3
Reputation: 6669
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;
}
Upvotes: 11