473,946 Members | 6,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Savannah2:Impos sible approach to a protected member.

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\Visu al 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\Visu al Studio
Projects\Savana \Animale.h(22): see the declaration of "Animale::x "
c:\Documents and Settings\Angelo \Documenti\Visu al Studio
Projects\Savana \Animale.h(4): vedere la dichiarazione di "Animale"
c:\Documents and Settings\Angelo \Documenti\Visu al 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\Visu al Studio
Projects\Savana \Animale.h(22): see the declaration of "Animale::y "
c:\Documents and Settings\Angelo \Documenti\Visu al 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(Anim ale &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_VI STA=50;
Animale::Animal e(double xx, double yy, double dire)
{
setX(x);
setY(y);
setDir(dire);
setStatus(true) ;
zonzo=30;
}
void Animale::setX(d ouble xx){
x=xx;
}
void Animale::setY(d ouble yy){
y=yy;
}

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

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

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

double Animale::getY() {
return y;
}
double Animale::getDir (){
return dir;
}
double Animale::getVis ta(){
return MAX_VISTA;
}
void Animale::print( ){
cout<<"Posizion e: ("<<x<<", "<<y<<")"<<endl ;
cout<<"Direzion e: "<<", "<<dir<<end l;
if(status==true )
cout<<"Ottima salute, è vivo"<<endl;
else
cout<<"Potrebbe andare meglio è morto"<<endl;
}

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

bool Animale::getSta tus(){
return status;
}

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

Animale::~Anima le(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::setSta tus(true);
resistenza=1000 00;
}
Leone::Leone(do uble xx, double yy, double dire):Animale(x x, yy, dire)
{
Animale::setSta tus(true);
resistenza=1000 00;
}
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(fal se);
resistenza=1000 00;
}
}
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(v oid)
{
}

#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::setSta tus(true);
}
Zebra::Zebra(do uble xx, double yy, double dire):Animale(x x, yy, dire)
{
Animale::setSta tus(true);
}

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

void Zebra::scappa(L eone &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(v oid)
{
}



Jul 22 '05 #1
4 1456
Piotre Ugrumov wrote:
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\Visu al 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\Visu al Studio
Projects\Savana \Animale.h(22): see the declaration of "Animale::x "
c:\Documents and Settings\Angelo \Documenti\Visu al Studio
Projects\Savana \Animale.h(4): vedere la dichiarazione di "Animale"
c:\Documents and Settings\Angelo \Documenti\Visu al 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\Visu al Studio
Projects\Savana \Animale.h(22): see the declaration of "Animale::y "
c:\Documents and Settings\Angelo \Documenti\Visu al 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(Anim ale &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_VI STA=50;
Animale::Animal e(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

--
Artie Gold -- Austin, Texas

Jul 22 '05 #2

"Artie Gold" <ar*******@aust in.rr.com> wrote in message:
...and, please, locate and RTFF!


That's nice -- I had never seen that one before. So I had to STFW.

Jonathan
Jul 22 '05 #3
On Sun, 18 Jan 2004 11:27:42 GMT in comp.lang.c++, "Piotre Ugrumov"
<au************ @tin.it> was alleged to have written:
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())){
Jul 22 '05 #4
On Mon, 19 Jan 2004 00:41:04 GMT in comp.lang.c++, David Harmon
<so****@netcom. com> was alleged to have written:
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
Jul 22 '05 #5

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

Similar topics

2
2328
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
4
3000
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new Derived());
5
1980
by: Andy Lomax | last post by:
Can anyone tell me why the code below doesn't compile? The code has a simple hierarchy of publically-derived classes: A -> B -> C. A declares a protected member 'foo'. C declares an object of type B, and attempts to access B.foo, which results in a compiler error: test.cpp: In member function `void C::test()': test.cpp:5: error: `int A::foo' is protected test.cpp:15: error: within this context
1
2002
by: karolszk | last post by:
Hi! The following program gcc returns error: class Z { int a; int b; };
16
3647
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
4
8512
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
12
2836
by: MiG | last post by:
Hello, Consider the following code snippet: class B { protected: B() {} ~B() {} };
15
3101
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248: 'base::~base' : cannot access protected member declared in
10
4134
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
10150
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9975
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10679
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7403
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4928
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 we have to send another system
3
3525
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.