lifeform.h
class lifeform
{
public:
struct item;
void buyItem(item &a);
//code..
};
lifeform.cpp
struct lifeform::item
{
std::string type,name;
bool own;
int value,feature;
item(std::string _type,std::string _name,int _value,int _feature):type(_type), name(_name),value(_value),feature(_feature)
{
own=false;
}
};
lifeform::item lBoots("boots","Leather Boots",70,20);
void lifeform::buyItem(item &a)
{
if(a.own==0)
{
inventory.push_back(a);
a.own=1;
addGold(-a.value);
std::cout << "Added " << a.name << " to the inventory.";
if(a.type=="boots")
{
hp-=inventory[1].feature;
inventory[1]=a;
std::cout << " ( HP + " << a.feature << " )\n";
maxHp+=a.feature;
hp+=a.feature;
}
}
there is no error so far but when i wanna use them in main.cpp like this
#include "lifeform.h"
int main()
{
lifeform p;
p.buyItem(lBoots);
}
compiler says me [Error] 'lBoots' was not declared in this scope but i declared it class am i missing something?
IGauntletsis.lGauntlets. Do you meanlBoots? And you need to declare is in the header file or no other translation unit will know about it.structandclassreally are the same thing in C++. The only difference is that struct default access level is public, so Inheritance and member are by default public, whereas class default level is private. Aside from that, there is no difference.lBoots