首页 > 解决方案 > 即使使用前向声明 (c++) 也会出现错误“X 不是类型”

问题描述

所以我在编译时收到了这个错误“Female is not a type”,即使我已经尝试在下面的代码中声明类Female:

男.hpp:

#ifndef GUARD_MALE_HPP
#define GUARD_MALE_HPP

#include <Elephant_Base.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <TextureHolder.hpp>
#include <memory>
#include <Female.hpp>
//class Female; --> doesn't work
class Male : public Elephant_Base {
public:
                    Male();
                    Male(Traits traits);
                    Male(TextureHolder& text, bool hasTusks, age_value a = 0);

    void            update(sf::Time dt);

    void            tryToMate(Female& f); //<--- shows error here

private:
    virtual void    draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

#endif

女性.hpp:

#ifndef GUARD_FEMALE_HPP
#define GUARD_FEMALE_HPP

#include <Elephant_Base.hpp>
#include <SFML/System/Time.hpp>
#include <TextureHolder.hpp>
#include <memory>


class Female : public Elephant_Base {
public:
                Female();
                Female(Traits traits);
                Female(TextureHolder& text, bool hasTusks, age_value a = 0);

    void            update(sf::Time dt);

    //female methods
    bool            canGetPregnant() const;
    bool            hasChildGrown() const;
    Elephant_Base::Ptr  detachChild();
    bool            isPregnant() const;

private:
    //female functions
    void            drawChild(sf::RenderTarget& target, sf::RenderStates states) const;
    void            makePregnant(const Elephant_Base::Gene& maleGamete);
    void            giveBirth(); //called in update; will create a child

private:
    virtual void    draw(sf::RenderTarget& target, sf::RenderStates states) const;

private:
    //female members
    Elephant_Base::Ptr mChild;
    bool               mIsPregnant;
    age_value          mGestationTime;  

};

#endif

我也试过没有前向声明,我仍然得到同样的错误,所以我有点迷失在这里应该做什么......

编辑:文件 Elephant_Base.hpp 已被询问,这里是:

#ifndef GUARD_ELEPHANT_BASE_HPP
#define GUARD_ELEPHANT_BASE_HPP

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include "TextureHolder.hpp"
#include <memory>

class Elephant_Base : public sf::Drawable, public sf::Transformable {
public:
    typedef float age_value;
    typedef std::shared_ptr<Elephant_Base> Ptr;
    const static sf::Time YEAR_IN_SECONDS_VALUE; //represents the value of a year in a certain amount of seconds
    //enum to define if the Elephant is going to be male or female hence determining the sexual chromosomes
    enum Sex {
        Male,
        Female,
        None,
    };

    //it's actually the sexual chromosomes, but this app aims to study the gene that grows tusks on the elephants
    //Xi is dominant and is the gene that grows tusks
    enum Gene {
        Xi,
        Xi_,
        Y,
        Undefined,
    };

    struct Traits {
        Traits();
        Traits(TextureHolder& text, Sex s, bool hasTusks, age_value a = 0);
        Traits(TextureHolder& text, std::pair<Gene, Gene> g, age_value a = 0);

        Sex                     sex;
        std::pair<Gene, Gene>   genes;
        bool                    tusks, alive;
        age_value               age;

        TextureHolder*          textures;

        static bool hasTusks(std::pair<Gene, Gene> g);
    private:
        //generates random pair of Gene depending on the elephant's sex and tusks
        void                    generateGenes(Sex sex, bool hasTusks);
        //generate random pair of genes depending on sex
        void                    generateRandomGenes(Sex sex);
    };

public:
                Elephant_Base();
                Elephant_Base(Traits traits);
                Elephant_Base(TextureHolder& text, Elephant_Base::Sex sex, bool hasTusks, age_value a = 0);
    virtual        ~Elephant_Base();

    //general methods
    virtual void    update(sf::Time dt) = 0;

    void            setEnclosure(sf::IntRect limits); //sets a limit range in which the Elephant can move
    void            kill();

    bool            hasTusks() const; //returns true if mTraits.tusks = true, hence Elephant has tusks
    bool            isAlive() const;

    sf::FloatRect   getBoundaries() const;
    Traits          getTraits() const;//returns mTraits
    static Gene     randomGeneX();    //returns either Gene::Xi or Gene::Xi_ with equal chances for both

protected:
    //general functions
    void            toAge(sf::Time dt);
    Gene            generateGametes() const; //generates a random Gene amongst owned ones
    void            updateMovement(sf::Time dt);

protected:
    sf::Sprite      mShape;

private:
    virtual void    draw(sf::RenderTarget& target, sf::RenderStates states) const;


private:
    //general members
    Traits             mTraits;
    sf::Vector2f       mMovement;
    sf::IntRect        mEnclosure;  
};

#endif

编辑2:有些人指出可能之前已经宣布了女性,实际上我已经在枚举中有一个Sex::Female。所以我必须更改两者之一的名称。

标签: c++forward-declaration

解决方案


推荐阅读