【问题标题】:expected class-name before '{' token, class inheritance'{' 标记之前的预期类名,类继承
【发布时间】:2016-01-07 21:36:28
【问题描述】:

我的项目文件有问题,我猜是由某些包含问题引起的。代码本身运行良好,但由于我更改了其他一些文件中的某些行,所以我在 '{' token' 错误之前得到了“预期的类名”。我已经尝试使用前向声明,但错误更改为“无效使用不完整类型‘类实体’”(帖子末尾的构建报告行)。

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_

错误''{'令牌'之前的预期类名:

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{
                       ^

错误'不完整类型的无效使用':

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
       ^

我不知道您需要哪些包含的文件来帮助我,但错误列表中列出的文件中没有一个包含文件 Entity.h 的包含。

非常感谢你,希望你能帮助我:)

【问题讨论】:

  • 您不能使用前向声明从类派生。基类的定义必须可以从它派生。尝试#includeing 定义class Entity 的头文件。
  • 每个包含文件都应该有一个对应的 .cpp 来包含它,即使它是空的,这也将有助于发现基本包含中的问题,而不是等待它被包含在其他地方。如果这样做,您可能会在另一个包含文件中发现导致此问题的问题。
  • 在您最近所做的更改中,您没有将EntityPlayer.h 包含在Entity.h 中吗?如果The code itself worked fine but since I changed some lines...,那么,您为什么不撤消更改以返回到正常工作的状态,然后一一重复更改以识别导致错误的更改?然后,您可以通过良好的投入发布更好的问题。
  • 实体类包含在“#include”中,所有包含的文件都有一个 .cpp 文件,到目前为止,实体类工作正常,我在错误发生之前没有改变任何东西,一切都很好
  • 而且我没有更改实体或 EntityPlayer 文件本身中的任何内容,但在包括这些在内的其他文件中,我无法撤消更改,因为我昨天试图自己解决问题,但我没有在更改之前备份代码

标签: c++ windows forward-declaration incomplete-type expected-exception


【解决方案1】:

只是将其标记为已回答。解释讨论:检查您的包含链以查看您是否包含来自 Entity.h 的 EntityPlayer.h。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多