Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Why Is There "Unknown Type" When Class Header is Included? [duplicate]

Tags:

c++

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 */
like image 345
j76goatboy Avatar asked May 05 '16 01:05

j76goatboy


2 Answers

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.

like image 151
MikeCAT Avatar answered Oct 21 '22 06:10

MikeCAT


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

like image 3
songyuanyao Avatar answered Oct 21 '22 08:10

songyuanyao