Help: Simulation of the life in the savannah

P

Piotre Ugrumov

in this simulation I have implemented 4 classes, a class Animal(Animale),
the classes Lion(Leone) and Zebra.
A Lion died after 100000 iteration if he don't eat a zebra.
If the distance between the lion and the zebra in less than 50, the lion
chases the zebra and the zebra escapes from the lion.
I have tried to create these classes int thsi way, but the compiler give me
9 errors, how can i resolve this errors?
Here there are the classes and the errors.
Thanks.

#pragma once

class Animale
{
public:
Animale(double xx, double yy, double dire);
void setX(double xx);
void setY(double yy);
void setVel(double vel);
void setDir(double dire);
void setStatus(bool);
double getVista();
double getX();
double getY();
double getDir();
bool getStatus();
virtual void print();
~Animale(void);
protected:
double distanza (Animale &a);
double angoloCong(Animale &a);
double x, y, dir;
static const double MAX_VISTA;
bool status;
int zonzo;
private:
double dist;
};



#include "StdAfx.h"
#include ".\animale.h"
#include <iostream>
#include <cmath>
using namespace std;


const double Animale::MAX_VISTA=50;
Animale::Animale(double xx, double yy, double dire)
{
setX(x);
setY(y);
setDir(dire);
setStatus(true);
zonzo=30;
}


void Animale::setX(double xx){
x=xx;
}
void Animale::setY(double yy){
y=yy;
}



void Animale::setDir(double dire){
dir=dire;
}

void Animale::setStatus(bool s){
status=s;
}

double Animale::getX(){
return x;
}

double Animale::getY(){
return y;
}


double Animale::getDir(){
return dir;
}


double Animale::getVista(){
return MAX_VISTA;
}
void Animale::print(){
cout<<"Posizione: ("<<x<<", "<<y<<")"<<endl;
cout<<"Direzione: "<<", "<<dir<<endl;
if(status==true)
cout<<"Ottima salute, è vivo"<<endl;
else
cout<<"Potrebbe andare meglio è morto"<<endl;
}

double Animale::distanza(Animale &a){
dist=sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
return dist;
}

bool Animale::getStatus(){
return status;
}

double Animale::angoloCong(Animale &a){
double dx=x-a.x;
double dy=y-a.y;
double angolo = atan2(dy, dx);
return angolo;
}

Animale::~Animale(void)
{
}





#pragma once
#include "animale.h"
#include "Zebra.h"

class Leone :
public Animale
{
public:
Leone();
Leone(double xx, double yy, double dire);
double getVel();
double getCor();
void vivi();
void insegui(Zebra &);
virtual void print();
~Leone(void);
protected:
void muovi();
static const double v, c;
long resistenza;
};





#include "StdAfx.h"
#include ".\leone.h"
#include "Zebra.h"
#include <cmath>
#include <iostream>
using namespace std;


const double Leone::c=14;
const double Leone::v=1.2;

Leone::Leone():Animale(0, 0, 0){
Animale::setStatus(true);
resistenza=100000;
}
Leone::Leone(double xx, double yy, double dire):Animale(xx, yy, dire)
{
Animale::setStatus(true);
resistenza=100000;
}


double Leone::getVel(){
return v;
}

double Leone::getCor(){
return c;
}

void Leone::insegui(Zebra &z){
x+=cos(Animale::angoloCong(z))*c;
y+=sin(Animale::angoloCong(z))*c;

if(Animale::x==z.x && Animale::y==z.y){
z.setStatus(false);
resistenza=100000;
}
}


void Leone::muovi(){
x+=cos(dir)*v;
y+=sin(dir)*v;
}

void Leone::vivi(){
--zonzo;
--resistenza;
if(zonzo!=0 && reistenza!=0){
muovi();
}else if(zonzo==0 && resistenza!=0){
setDir(rand() % 360);
}else if(resistenza==0)
setStatus(false);
}

void Leone::print(){
cout<<"Leone: "<<endl;
Animale::print();
}

Leone::~Leone(void)
{
}



#pragma once
#include "animale.h"
#include "Leone.h"

class Zebra :
public Animale
{
public:
Zebra();
Zebra(double xx, double yy, double dire);
void scappa(Leone &);
void vivi();
virtual void print();
~Zebra(void);
protected:
void muovi();
static const double v, c;
};



#include "StdAfx.h"
#include ".\zebra.h"
#include "Leone.h"
#include <cmath>
#include <iostream>
using namespace std;



const double Zebra::v=1.1;
const double Zebra::c=10;

Zebra::Zebra():Animale(0, 0, 0){
Animale::setStatus(true);
}
Zebra::Zebra(double xx, double yy, double dire):Animale(xx, yy, dire)
{
Animale::setStatus(true);
}



void Zebra::muovi(){
x+=cos(dir)*v;
y+=sin(dir)*v;
}

void Zebra::scappa(Leone &l){
x+=cos(Animale::angoloCong(l))*c;
y+=sin(Animale::angoloCong(l))*c;
}

void Zebra::vivi(){
--zonzo;
if(zonzo!=0){
muovi();
}else
setDir(rand() % 360);
}

void Zebra::print(){
cout<<"Zebra: "<<endl;
Animale::print();
}

Zebra::~Zebra(void)
{
}




#pragma once
#include "Leone.h"
#include "Zebra.h"
class Savan
{
public:
Savan();
void addLeone(Leone &);
void addZebra(Zebra &);
int getLeoni();
int getZebre();
void simula (long);
~Savan(void);
private:
//I must use array of pointer
Leone *lPtr;
int lsize;
Zebra *zPtr;
int zsize;
};




#include "StdAfx.h"
#include ".\savan.h"

Savan::Savan(void)
{
lsize=0;
zsize=0;
lPtr=0;
zPtr=0;
}


void Savan::addLeone(Leone &l){
Leone *tmp;
tmp=lPtr;
lsize=lsize+1;
lPtr=new Leone[lsize];
lPtr=tmp;
lPtr[lsize-1]=l;
delete[] tmp;
}

void Savan::addZebra(Zebra &z){
Zebra *tmp;
tmp=zPtr;
zsize=zsize+1;
zPtr=new Zebra[zsize];
zPtr=tmp;
zPtr[zsize-1]=z;
delete[] tmp;
}

int Savan::getLeoni(){
return lsize;
}

int Savan::getZebre(){
return zsize;
}

void Savan::simula(long n){
for(int i=0; i<n; i++){
for(int j=0; j<lsize; j++){
for(int k=0; k<zsize; k++){
if(lPtr[j].getStatus()==true && zPtr[k].getStatus()==true){
lPtr[j].vivi();
zPtr[k].vivi();
if(lPtr[j].distanza(zPtr[k])<lPtr[j].getVista()){
lPtr[j].insegui(zPtr[k]);
zPtr[k].scappa(lPtr[k]);
}
}
}
}
}
}
Savan::~Savan(void)
{
}



------ Inizio generazione: Progetto: Savana, Configurazione: Debug
Win32 ------

Compilazione in corso...
Zebra.cpp
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.h(14) : error C2061: syntax error: identifier "Zebra"
Savan.cpp
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Zebra.h(11) : error C2061: syntax error: identifier "Leone"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Savan.cpp(48) : error C2248: "Animale::distanza": impossible
approach to protected member declared in the class "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(20): see the declaration of "Animale::distanza"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(4): see the declaration of "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Savan.cpp(50) : error C2660: "Zebra::scappa": the function
doesn't accept 1 arguments
Leone.cpp
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Zebra.h(11) : error C2061: syntax error: identifier "Leone"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(38) : error C2248: "Animale::x": impossible
approach to protected member declared in the class "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(22): see the declaration of "Animale::x"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(4): see the declaration of "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(38) : error C2248: "Animale::y":impossible
approach to protected member declared in the class "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(22): see the declaration of "Animale::y"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Animale.h(4): see the declaration of "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(53) : error C2065: "reistenza": identifier doesn't
declared
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.h(15): see the previous definition of"print"
 
D

David Harmon

I have tried to create these classes int thsi way, but the compiler give me
9 errors, how can i resolve this errors?

When header "Leone.h" needs header "Zebra.h" and simultaneously the
reverse, "#pragma once" is not going to sort it out. Put simple forward
declarations in your header files for the classes you reference there.
Example:

// Leone.h
#include "animale.h"
class Zebra; // instead of #include "zebra.h"

This issue is covered in the topic "[34.9] How can I create two classes
that both know about each other?" of Marshall Cline's C++ FAQ. It is
always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top