-1

I have a problem with a file of my project, i guess caused by some include issues. The code itself worked fine but since I changed some lines in some other files I get a 'expected class name before '{' token' error. I've already tried to use forward declarations but with them the error changes to 'invalid use of incomplete type 'class Entity'' (lines of build report at the end of the post).

EntityPlayer.h

#ifndef _SHARED_GAME_ENTITY_ENTITYPLAYER_H_
#define _SHARED_GAME_ENTITY_ENTITYPLAYER_H_

//////////////////////////////////////////////////
//////////////////// INCLUDES ////////////////////
//////////////////////////////////////////////////


#include<array>
#include<iostream>
#include<windows.h>
#include<SFML/System.hpp>

#include<Shared/Game/Entity/Entity.h>
#include<Shared/Game/ItemStack.h>
#include<Shared/Game/Items.h>
#include<Shared/System/Types.h>

//////////////////////////////////////////////////
////////// STRUKTUREN; KLASSEN; OBJEKTE //////////
//////////////////////////////////////////////////

//class Entity; //forward declaration of 'class Entity' -> invalid use of incomplete type 'class Entity' line 27
class ItemStack;
class Items;

class EntityPlayer : public Entity{ //line 27
public:
    struct PlayerStats{
        int health;
    };

    EntityPlayer(const std::string ID, const GameMode mode);
    virtual ~EntityPlayer();
    virtual void                       update();
    virtual void                       setGameMode(const GameMode mode);
    virtual bool                       isInventoryOpen();
    virtual bool                       isInInventory(ItemStack* item);
    virtual bool                       removeFromInventory(ItemStack *item);
    virtual uint                       addToInventory(ItemStack* item);
    virtual PlayerStats                getPlayerStats();
    virtual AABB                       getAABB();
    virtual std::array<ItemStack*, 10> getHotbarItems();
    virtual std::array<ItemStack*, 40> getInventory();

protected:
    virtual bool isInventorySlotFree();
    virtual bool isHotbarSlotFree();
    virtual bool addToHotbarSlot(ItemStack* itemstack);

    bool                       m_inventoryOpen;
    bool                       m_ltp;
    GameMode                   m_mode;
    PlayerStats                m_stats;
    std::array<ItemStack*, 10> m_itemsHotbar;
    std::array<ItemStack*, 40> m_inventory;
};

//////////////////////////////////////////////////
/////////////////// PROTOTYPEN ///////////////////
//////////////////////////////////////////////////



#endif // _SHARED_GAME_ENTITY_ENTITYPLAYER_H_

Entity.h

#ifndef _SHARED_GAME_ENTITY_ENTITY_H_
#define _SHARED_GAME_ENTITY_ENTITY_H_

//////////////////////////////////////////////////
//////////////////// INCLUDES ////////////////////
//////////////////////////////////////////////////

#include<map>
#include<iostream>
#include<windows.h>

#include<Shared/Game/World.h>
#include<Shared/System/Defines.h>
#include<Shared/System/Log.h>
#include<Shared/System/Types.h>

//////////////////////////////////////////////////
////////// STRUKTUREN; KLASSEN; OBJEKTE //////////
//////////////////////////////////////////////////

class World;

class Entity{
    friend class GameRegistry;

    #ifdef CLIENT
    friend class GameRenderer;
    #endif // CLIENT

public:
    Entity(const std::string ID, const std::string texture);
    virtual ~Entity();
    virtual void        update();
    virtual bool        linkWorld(World* world);
    virtual bool        isWorldLinked();
    virtual bool        setPosition(const Vec3d position);
    virtual bool        move(const Vec2zd value);
    virtual bool        move(const double x, const double z);
    virtual float       getRotation();
    virtual Vec3d       getPosition();
    virtual std::string getID();
    virtual std::string getTextureName();
    virtual std::string getTextureLocation();
    virtual AABB        getAABB();

protected:
    virtual int getNumericID();

    int         m_numericID;
    float       m_rotation;
    World*      m_world;
    Vec3d       m_position;
    std::string m_id;
    std::string m_texture;

public:
    static bool   isEntityRegistered(const std::string name);
    static Entity getEntityCopy(const std::string name);

protected:
    static std::map<std::string, Entity*> m_entitys;
};

//////////////////////////////////////////////////
/////////////////// PROTOTYPEN ///////////////////
//////////////////////////////////////////////////



#endif // _SHARED_GAME_ENTITY_ENTITY_H_

Error 'expected class-name before '{' token':

In file included from src/Shared/Game/CraftingManager.h:13:0,
                 from src/Shared/Game/GameRegistry.h:16,
                 from src/Shared/Game/Blocks.h:18,
                 from src/Shared/Game/World.h:12,
                 from src/Shared/Game/Entity/Entity.h:12,
                 from E:\MasterS\Projekte\C++\Spiele\Project 008\src\Shared\Game\Entity\Entity.cpp:1:
src/Shared/Game/Entity/EntityPlayer.h:28:35: error: expected class-name before '{' token
 class EntityPlayer : public Entity{
                       ^

Error 'invalid use of incomplete type':

In file included from src/Shared/Game/CraftingManager.h:13:0,
                 from src/Shared/Game/GameRegistry.h:16,
                 from src/Shared/Game/Blocks.h:18,
                 from src/Shared/Game/World.h:12,
                 from src/Shared/Game/Entity/Entity.h:12,
                 from E:\MasterS\Projekte\C++\Spiele\Project 008\src\Shared\Game\Entity\Entity.cpp:1:
src/Shared/Game/Entity/EntityPlayer.h:28:29: error: invalid use of incomplete type 'class Entity'
 class EntityPlayer : public Entity{
                             ^
src/Shared/Game/Entity/EntityPlayer.h:24:7: error: forward declaration of 'class Entity'
 class Entity; //forward declaration of 'class Entity' -> invalid use of incomplete type 'class Entity' line 27
       ^

I don't know which of the included files you'll need to help me but no one of the files listed in the error list contains the include of the file Entity.h.

Thank you very much in advance, hope you can help me :)

  • You cannot derive from a class using a forward declaration. The definition of the base class must be available to derive from it. Try `#include`ing the header file where `class Entity` is defined. – R Sahu Jan 07 '16 at 21:39
  • each include file should have a corresponding `.cpp` that includes it, even if it is otherwise empty this will help find problems in the base include and not wait for it to be included elsewhere. If you do that you might find an issue in another include file causing this issue. – Glenn Teitelbaum Jan 07 '16 at 21:44
  • In the changes you recently did, didn't you include `EntityPlayer.h` from `Entity.h`? If `The code itself worked fine but since I changed some lines...`, then, why don't you undo your changes to move back to something working and then repeat the changes one by one to identify the one leading to your error? Then you can post a better question with good inputs. – jpo38 Jan 07 '16 at 21:48
  • The class Entity is included in '#include' and all of the included files have a .cpp file and the Entity class works fine so far and I haven't changed anything in it before the error occured and there all went fine – mastermine Jan 07 '16 at 21:51
  • And I havent changed anything in the Entity or the EntityPlayer files itself but in other files including these and I can't undo the changes because I tried to figure the problem out by myself yesterday and I have no backup with the code before the changes – mastermine Jan 07 '16 at 21:52
  • Can you show what Entity.h has? – George Houpis Jan 07 '16 at 21:54
  • Names that begin with an underscore followed by a capital letter (`_SHARED_GAME_ENTITY_ENTITYPLAYER_H_`) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Jan 07 '16 at 21:58
  • Added Entity.h @Pete Becker thanks I will change it – mastermine Jan 07 '16 at 21:59
  • Do you include EntityPlayer.h anywhere in the include chain before class Entity is defined in Entity.h? Like in CraftingManager.h or GameRegistry.h or World.h? – George Houpis Jan 07 '16 at 22:14
  • Yes in CraftingManager.h – mastermine Jan 07 '16 at 22:21
  • @GeorgeHoupis Thank you very much I checked the CraftingManager.h file once more and found an forward declaration which is unnecessary since the changed. I removed the forward decleration and it's working now fine :) – mastermine Jan 07 '16 at 22:24

1 Answers1

0

Just to mark this as answered. Paraphrasing the discussion: Check your include chain to see if you are including EntityPlayer.h from Entity.h.

George Houpis
  • 1,729
  • 1
  • 9
  • 5