所以我在编译时遇到了“女性不是类型”的错误,即使我已经尝试通过以下代码声明女性类:
Male.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
Female.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。所以我必须更改两者之一的名称。