Probleme de forward declaration
re bonjour !
g voulu résoudre un problème d'inclusion circulaire avec une forward declaration
entre deux .h
Animal.h et Faune.h
du coté de Faune tout roule , mais pour animal g voulu implémenter une méthode utilitaire qui utilise faune et ses méthodes
mais voila les erreur que ces méthodes occasionnent:
In member function `void Lapin::avoirBebeLapin(Faune&, int)'
invalid use of undefined type `struct Faune'
forward declaration of `struct Faune'
Idem pour avoirBeBeRenard(Faune &, int)
voici les sources des .h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#ifndef _ANIMAL
#define _ANIMAL
#include "./Para.h"
#include "./Faune.h"
#include "./Monde.h"
#include <time.h>
#include <math.h>
#include <iostream>
using namespace std;
int refGenerale=0;
class Faune;
class Animal{
int wAge;
int wSex; // 0 male, 1 femelle
bool wIfDead;
int wEnergy;
int wRef;
bool wIfReprod;
char wType;
Herbe* wPos;
public:
Animal(){
cout<<"Constructeur d'Animal\n";
wAge = 0;
wIfDead = false;
wRef = refGenerale++;
wIfReprod = false;
wPos = NULL;
}
virtual ~Animal(){
cout<<"Destructeur d'animal\n";
if (wPos != NULL) delete wPos;
}
int getAge(){return wAge;}
int getSex(){return wSex;}
bool getIfDead(){return wIfDead;}
int getEnergy(){return wEnergy;}
int getRef(){return wRef;}
bool getIfReprod(){return wIfReprod;}
char getType(){return wType;}
int getX(){return wPos->X;}
int getY(){return wPos->Y;}
int getNbRabbit(){return wPos->nbRabbitMale + wPos->nbRabbitFemelle;}
int getNbFox(){return wPos->nbFoxMale + wPos->nbFoxFemelle;}
int getHauteurHerbe(){return wPos->hauteurHerbe;}
Herbe* getPos(){return wPos;}
void vieillir(){wAge = wAge+1;}
void setSex(int s){wSex = s;}
void setIfDead(bool d){ wIfDead = d;}
void setEnergy(int e){ wEnergy = e;}
void addEnergy(int e){wEnergy = wEnergy + e;}
void subEnergy(int e){wEnergy = wEnergy - e;}
void setIfReprod(bool r){wIfReprod = r;}
char setType(char c){wType = c;}
void setPos(Herbe* h){wPos = h;}
virtual void trySeDeplacer(Monde &) = 0;
virtual void trySeReproduire(Faune &) =0;
virtual void tryManger(Faune &) =0;
};
class Lapin:public Animal{
public:
Lapin():Animal(){
cout<<"Constructeur de Lapin \n";
Animal::setType('r');
Animal::setEnergy(tabInfo[9]);
}
Lapin(int s):Animal(){
Animal::setType('r');
Animal::setEnergy(tabInfo[9]);
Animal::setSex(s);
}
~Lapin(){cout<<"Destructeur de Lapin\n";}
void avoirBebeLapin(Faune &f, int n){
int i = 0;
while( i != n){
Lapin* l = new Lapin(rand()%2);
l->setPos(this->getPos());
f.addAnimal(l);
tabRapport[7]++;
i++;
}
}
void trySeDeplacer(Monde &m){} //à définir
void trySeReproduire(Faune &f){} //à définir
void tryManger(Faune &f){cout<<"Miam une carotte\n";}//à définir
};
class Renard:public Animal{
public:
Renard():Animal(){
cout<<"Constructeur de renard \n";
Animal::setType('f');
Animal::setEnergy(tabInfo[10]);
}
Renard(int s):Animal(){
Animal::setType('f');
Animal::setEnergy(tabInfo[10]);
Animal::setSex(s);
}
~Renard(){cout<<"Destructeur de Renard\n";}
void avoirBebeRenard(Faune &f, int n){
int i = 0;
while( i != n){
Renard* r = new Renard(rand()%2);
r->setPos(this->getPos());
f.addAnimal(r);
tabRapport[8]++;
i++;
}
}
void trySeDeplacer(Monde &m){} //à définir
void trySeReproduire(Faune &f){} //à définir
void tryManger(Faune &f){cout<<"miam un lapin\n";}//à définir
};
#endif // _ANIMAL |
et faune.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#ifndef _FAUNE
#define _FAUNE
#include "./Animal.h"
#include "./Para.h"
#include <vector>
#include <iostream>
using namespace std;
typedef vector<Animal*> VecAnim;
class Animal;
class Faune:private VecAnim{
public:
Faune():VecAnim(){cout<<"constructeur de faune\n";}
~Faune(){
cout<<"Destructeur de faune\n";
VecAnim::iterator i;
for(int i=0;i<this->getSize();i++)delete this->operator[](i);
this->clear();
}
int getSize(){return this->size();}
void addAnimal(Animal* a)
{
this->push_back(a);
//si lapin
if( a->getType() == 'r'){
//si male
if(a->getSex()== 0) a->getPos()->nbRabbitMale++;
//si femelle lapin
else a->getPos()->nbRabbitFemelle++;
}
else{//si renard
//si male
if(a->getSex()== 0) a->getPos()->nbFoxMale++;
//si femelle renard
else a->getPos()->nbFoxFemelle++;
}
}
void delAnimal(int ref)
{
int i=0;
VecAnim::iterator ite;
ite=this->begin();
while (i != this->getSize() && this->operator[](i)->getRef() != ref) {
i++;ite++;}
if( i!=this->getSize())
{
//si lapin
if( this->operator[](i)->getType() == 'r'){
//si male
if(this->operator[](i)->getSex()== 0) this->operator[](i)->getPos()->nbRabbitMale--;
//si femelle lapin
else this->operator[](i)->getPos()->nbRabbitFemelle--;
}
else{//si renard
//si male
if(this->operator[](i)->getSex()== 0) this->operator[](i)->getPos()->nbFoxMale--;
//si femelle renard
else this->operator[](i)->getPos()->nbFoxFemelle--;
}
delete this->operator[](i);
this->operator[](i)=NULL;
this->erase(ite,ite);
}
}
Animal* seekAnimal(int ref)
{
int i=0;
while (i != this->getSize() && this->operator[](i)->getRef() != ref) i++;
if( i!=this->getSize()) return this->operator[](i);
}
Animal* & operator[](int i)
{
return *(begin() + i);
}
// verif si la bestiole à fait son temps
bool ifDeadAge(int i){
//si Lapin
if(this->operator[](i)->getType() == 'r'){
if( this->operator[](i)->getAge()> tabInfo[1]) return true;
}
else{// si Renard
if( this->operator[](i)->getAge()> tabInfo[2]) return true;
}
return false;
}
// fin de tour : mettre reprod a false , vieillir, mourir les vieux,
void finDeTour(){
int i;
for( i=0;i != this->getSize();i++){
this->operator[](i)->vieillir();
if( ifDeadAge(i)) this->delAnimal(this->operator[](i)->getRef());
if(this->operator[](i)->getSex() == 1 && this->operator[](i)->getIfReprod() == true)
this->operator[](i)->setIfReprod(false);
}
}
};
#endif //_FAUNE |
voila je pensais pourtant que ça aller passer , enfin je maitrise bien mal tout ce qui est inclusion !
Si quelqu'un à une solution ou indication merci d'avance !! ;)