473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help: Simulation of the life in the savannah

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"

Jul 22 '05 #1
1 1504
On Sat, 17 Jan 2004 17:22:36 GMT in comp.lang.c++, "Piotre Ugrumov"
<au************@tin.it> was alleged to have written:
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/
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Constandinos Mavromoustakis | last post by:
Dear all, first we apologize if you receive multiple copies of this announcement. please see below if you are interested. Thank you in advance....
0
by: Constandinos Mavromoustakis | last post by:
http://agent.csd.auth.gr/~cmavrom -------------------------------------------------- ============================================================================ = 37th Annual Simulation...
13
by: mgorbach | last post by:
Im writing a program that does monte carlo simulation and im having trouble figuring out how to get the threading model right. I have a simulation class which contains all simulation data and...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
2
by: maxwin | last post by:
Ps i need help about how to create a simulation of doodlebugs life Problem: anaylysis The goal for this problem is to create a simple two-dimensional predator-prey simulation. In the simulation...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
4
by: lam388 | last post by:
Write a program to implement the simulation of life as described in Scientific American by Martin Gardner. The program will be implemented on a two dimensional surface of size 80 by 80 visible...
2
by: lam388 | last post by:
Write a program to implement the simulation of life as described in Scientific American by Martin Gardner. The program will be implemented on a two dimensional surface of size 80 by 80 visible...
3
by: Adem | last post by:
Projects looking for Developer Help Wanted - 174 Developer - 9 Project Manager - 1 Unix Admin - 47 Doc Writer - 29 Tester - 4 Support Manager - 27 Graphic/Other Designer
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.