I have this header file and I'm trying to make variables of type Item
. I've included #include "Item.h"
and yet I still get a unknown type name Item
error on both private variables when I compile.
#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
#include "Item.h"
using std::vector;
class Player
{
public:
// constructor
Player( void );
// destructor
virtual ~Player( void );
private:
Item item;
std::vector <Item> inventory;
};
#endif /* PLAYER_H */
Whats up with this?
Heres the Item.h
that I'm including
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include "Player.h"
#include "GlobalDefs.h"
class Item {
public:
Item();
Item(gold_t v, std::string n);
virtual ~Item();
// Getter
inline virtual gold_t GetValue (void)
{
return value;
}
// Getter
inline virtual std::string GetName (void);
// Getter
virtual std::string GetItemText(void);
protected:
gold_t value;
std::string name;
};
#endif /* ITEM_H */
If you include Item.h
from your cpp
file, Player.h
is included from it. Then, Player.h
includes Item.h
again, but thanks to the include guard, this does virtually nothing.
Then, in the included Player.h
, no Item
is declared yet. Therefore, the compiler will emit the error.
Since nothing from Player.h
is used in Item.h
, remove #include "Player.h"
from Item.h
.
You're including "Player.h"
in "Item.h"
and make it circular dependency. Since it's not necessary at all so just remove it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With