16

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 */
j76goatboy
  • 405
  • 2
  • 7
  • 16
  • Note: methods defined inside the class body are automatically inline so the `inline` keyword on `GetValue` is redundant. Also marking 0-parameter parameter lists as `void` is redundant too. – Emil Laine May 05 '16 at 01:21
  • In the top of your code you have written "using std::vector". Isn't it unnecessary to declare a vector with "std::"? – Abdel Aleem Dec 04 '19 at 09:32

2 Answers2

14

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.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
3

You're including "Player.h" in "Item.h" and make it circular dependency. Since it's not necessary at all so just remove it.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405