Savannah2:Impossible approach to a protected member.

P

Piotre Ugrumov

I have tried to modify my exercise about the simulation of the life in the
savannah. Now I have only 2 errors but I don't comprehend how resolve these
errors. If I try to call the method getX() and getY() of the class Animale
from the class Leone, the compiler return to me the same errors.
These are the errors:
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(36) : error C2248: "Animale::x": impossible to
enter to a 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): vedere la dichiarazione di "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(36) : error C2248: "Animale::y": impossible to
enter to a 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 "Animal

How can I resolve definitively the problems?
Thanks.

Here there are the classes Animale, Leone and Zebra:
#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();
double distanza (Animale &a);
virtual void print();
~Animale(void);
protected:
double angoloCong(Animale &a);
double x, y, dir;
static const double MAX_VISTA;
bool status;
int zonzo;
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"
class Zebra;

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((x==z.x) && (x==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 && resistenza!=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)
{
}
 
A

Artie Gold

Piotre said:
I have tried to modify my exercise about the simulation of the life in the
savannah. Now I have only 2 errors but I don't comprehend how resolve these
errors. If I try to call the method getX() and getY() of the class Animale
from the class Leone, the compiler return to me the same errors.
These are the errors:
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(36) : error C2248: "Animale::x": impossible to
enter to a 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): vedere la dichiarazione di "Animale"
c:\Documents and Settings\Angelo\Documenti\Visual Studio
Projects\Savana\Leone.cpp(36) : error C2248: "Animale::y": impossible to
enter to a 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 "Animal

How can I resolve definitively the problems?
Thanks.

Here there are the classes Animale, Leone and Zebra:
#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();
double distanza (Animale &a);
virtual void print();
~Animale(void);
protected:
double angoloCong(Animale &a);
double x, y, dir;
static const double MAX_VISTA;
bool status;
int zonzo;
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); ITYM:
setX(xx);
setY(y);
ITYM:
setY(yy);

Of course you *should* be doing all this in an initialization list.
setDir(dire);
setStatus(true);
zonzo=30;
}
[snip]

In the future, when posting here, please provide *only* the smallest
possible compilable snippet (or, in the case of syntax/constraint
problems, the smallest snippet you *think* should be compilable) --
*without* the use of system specific, erm, extensions.

Also indicate the line(s) that trigger(s) the error.

....and, please, locate and RTFF!

HTH,
--ag
 
D

David Harmon

If I try to call the method getX() and getY() of the class Animale
from the class Leone, the compiler return to me the same errors.
These are the errors:
Leone.cpp(36) : error C2248: "Animale::x": impossible to
enter to a protected member declared in the class "Animale"

It was hard to locate line 36 of Leone.cpp in your post since you did
not even indicate where the beginning of each file was. Of course it is
sometimes impossible to extract the minimum code to diagnose the error,
if you don't understand the error, and too much is better than too
little. But if you must post multiple files, please put a comment like
"// Leone.h" at the beginning of each file, and if possible indicate the
line the error message refers to.

This issue is covered in topic "[5.7] How do I post a question about
code that doesn't work correctly?" 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/

The error refers to:
void Leone::insegui(Zebra &z){
if((x==z.x) && (x==z.y)){

It is a mystery to me why those are both "x==".

Class Leone has access to x and y members of Leone objects since they
are protected in base class Animale. But, it has no access to x and y
members of class Zebra, because Zebra owns those and is responsible for
maintaining the integrity of them. If Leone had access, they could be
set to values unsuitable for class Zebra, same as if they were public,
and Zebra could do nothing about it.

What exactly did you try with getX() and getY()? It should have worked
if the code was like
if((x==z.getX()) && (y==z.getY())){
 
D

David Harmon

It should have worked if the code was like
if((x==z.getX()) && (y==z.getY())){

Change "should have worked" to "should have compiled."

I notice that x and y and the rest are doubles. It will never be right
as long as you are comparing floating point numbers with ==. Instead
you must design some suitable test for whether they are "close enough"
according to the requirements of your program.

The usual answer to that is the same in C++ as it is in C, and is
covered topic "14.5: What's a good way to check for "close enough"
floating-point" of in Steve Summit's C FAQ. Read all of section 14.
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.eskimo.com/~scs/C-faq/top.html
 

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
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top