无效使用不完整类型/转发声明

riv*_*von 15 c++ gcc forward-declaration incomplete-type

我试着看看Stackoverflow和谷歌上列出的类似问题,但他们主要处理模板,这不是我的情况.我在Debian测试64位上使用GCC 4.4.5.
所以,我有两个类 - CEntity:

#ifndef CENTITY_H_INCLUDED
#define CENTITY_H_INCLUDED

#include "global_includes.h"

// game
#include "CAnimation.h"
#include "Vars.h"
#include "vector2f.h"
#include "Utils.h"

class CAnimation;

class CEntity
{
public:
    CEntity();
    virtual ~CEntity();

    void _update(Uint32 dt);

    void updateAnimation(Uint32 dt);

    void addAnimation(const std::string& name, CAnimation* anim);
    void addAnimation(const std::string& name, const CAnimation& anim);
    void removeAnimation(const std::string& name);
    void clearAnimations();

    bool setAnimation(const std::string& name);

    SDL_Surface* getImage() const;

    const vector2f& getPos() const;
    const vector2f& getLastPos() const;
    F getX() const;
    F getY() const;
    F getLastX() const;
    F getLastY() const;
    SDL_Rect* getHitbox() const;
    SDL_Rect* getRect() const;

    F getXSpeed() const;
    F getYSpeed() const;

    void setPos(const vector2f& pos);
    void setPos(F x, F y);
    void setPos(F n);
    void setX(F x);
    void setY(F y);

    void setHitboxSize(int w, int h);
    void setHitboxSize(SDL_Rect* rect);
    void setHitboxWidth(int w);
    void setHitboxHeight(int h);

    void setSpeed(F xSpeed, F ySpeed);
    void setXSpeed(F xSpeed);
    void setYSpeed(F ySpeed);

    void stop();
    void stopX();
    void stopY();

    void affectByGravity(bool affect);

    void translate(const vector2f& offset);
    void translate(F x, F y);

    bool collide(CEntity& s);
    bool collide(CEntity* s);

protected:
    CAnimation* mCurrentAnimation;
    SDL_Surface* mImage;

    vector2f mPos;
    vector2f mLastPos;
    SDL_Rect* mHitbox; // used for collisions
    SDL_Rect* mRect; // used only for blitting

    F mXSpeed;
    F mYSpeed;

    bool mAffByGrav;

    int mHOffset;
    int mVOffset;

private:
    std::map<std::string, CAnimation*> mAnims;
};

#endif // CENTITY_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

和CPlayerChar继承自CEntity:

#ifndef CPLAYERCHAR_H_INCLUDED
#define CPLAYERCHAR_H_INCLUDED

#include "global_includes.h"

// game
#include "CEntity.h"

class CEntity;

class CPlayerChar : public CEntity
{
public:
    CPlayerChar();
    virtual ~CPlayerChar();

    virtual void update(Uint32 dt) = 0;

    virtual void runLeft() = 0;
    virtual void runRight() = 0;
    virtual void stopRunLeft() = 0;
    virtual void stopRunRight() = 0;

    virtual void attack() = 0;
    virtual void stopAttack() = 0;

    virtual void attack2() = 0;
    virtual void stopAttack2() = 0;

    virtual void ground() = 0;
    virtual void midair() = 0;

    void jump();
    void stopJump();

protected:
    // looking right?
    bool mRight;

    bool mJumping;
    bool mOnGround;
    bool mGrounded;
};

#endif // CPLAYERCHAR_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,GCC会抛出此错误:

CPlayerChar.h:12: error: invalid use of incomplete type ‘struct CEntity’
CPlayerChar.h:9: error: forward declaration of ‘struct CEntity’
Run Code Online (Sandbox Code Playgroud)

我没有前进声明'class CEntity'就先试了一下.在第9行的CPlayerChar.h中,但它会抛出它

CPlayerChar.h:12: error: expected class-name before ‘{’ token
Run Code Online (Sandbox Code Playgroud)

所以前方声明必须在那里.此外,CEntity显然是一个类,而不是结构.

Mar*_*ork 10

您的头文件中包含循环包含.
但是如果没有所有头文件,我们将无法修复它.

我会从这里开始.

#include "CAnimation.h"
Run Code Online (Sandbox Code Playgroud)

看看你的标题,你实际上并不需要这个.您只能通过引用或指针使用CAnimation,因此您的前向声明应该足够了.将include移动到源文件中(即在标题之外).

我想看的下一个地方是:

#include "global_includes.h"
Run Code Online (Sandbox Code Playgroud)

包含在头文件中的任何全局包含都非常简单.应该只包含简单类型而不包含任何其他头文件(除非它们一样简单).任何复杂的东西都会导致循环依赖的问题.

一般经验法则

头文件应该只包含它绝对需要的头文件.否则,它们应该包含在源文件中.如果它定义了一个用作父类的类,或者您使用该类的参数对象,那么您只需要一个头文件.

我使用该术语object来区分引用或指针.如果您使用这些,则不需要包含头文件.你只需要做一个前瞻声明.


And*_*uel 6

你可能在你的包含中有一个循环,CPlayerChar不知道谁真的是CEntity,它只知道它存在,但不知道它是什么.

如果删除"class CEntity"声明,您将看到GCC会抱怨CEntity不存在.

您必须检查CEntity包含的内容是否包括CPlayerChar.


Ker*_* SB 5

您必须确保在您定义类的地方可以看到类的完整定义。(所以检查你的内含物。)CEntityCPlayerChar

这是因为您只能从完全定​​义的类继承,而不能从前向声明的类继承。

唯一可以使用前向声明代替完整定义的时间是当您对类型进行指针或引用时,但前提是您从不访问其任何成员,或者(感谢@Alf)在声明函数时不完整的返回类型。