c – 无效使用不完整类型/前向声明

前端之家收集整理的这篇文章主要介绍了c – 无效使用不完整类型/前向声明前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图看看Stackoverflow和Google上列出的类似问题,但他们主要处理模板,这不是我的情况.我在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

和继承自CEntity的CPlayerChar:

#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

当我尝试编译它,GCC抛出这个错误

CPlayerChar.h:12: error: invalid use of incomplete type ‘struct CEntity’
CPlayerChar.h:9: error: forward declaration of ‘struct CEntity’

我没有前向声明“CEntity类”在第9行的CPlayerChar.h中,但是它会抛出它

CPlayerChar.h:12: error: expected class-name before ‘{’ token

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

解决方法

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

如果您删除“类CEntity”声明,您将看到GCC将抱怨CEntity不存在.

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

原文链接:https://www.f2er.com/c/112944.html

猜你在找的C&C++相关文章