1

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?

4
  • 1
    It may be relevant to share what IGauntlets is. Commented Apr 19, 2017 at 14:25
  • I don't see any definition of lGauntlets. Do you mean lBoots? And you need to declare is in the header file or no other translation unit will know about it. Commented Apr 19, 2017 at 14:28
  • @RedPotato remember that struct and class really 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. Commented Apr 19, 2017 at 14:29
  • @Someprogrammerdude yeah yeah its lBoots Commented Apr 19, 2017 at 14:29

1 Answer 1

1

To use your lifeform::item lBoots you need to declare it in main:

#include "lifeform.h"

extern lifeform::item lBoots; // <-- you need this.

int main()
{
    lifeform p;
    p.buyItem(lBoots);
}

Or alternatively you should place extern lifeform::item lBoots; in your lifeform.h.

Sign up to request clarification or add additional context in comments.

6 Comments

The problem with op is not lifeform::item to be an incomplete type, but to expose lBoots as an extern variable in his header.
@GuillaumeRacicot there is no problem with lifeform::item , the problem is i declared lBoots in class.cpp but cant use in main.cpp
@GuillaumeRacicot yes I see it now, I think the question got edited.
@RedPotato I updated the answer, you need to declare lBoots in main.cpp to be able to reference it.
@Pavel the declaration of extern lifeform::item lBoots belongs in lifeform.h. edit that and the upvote is yours.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.