Conditionally initiating objects

J

jeroendeurinck

Hi all,

I'm a newbe, so sorry if this question would be inappropriate here.
Nevertheless I try.
---
Suppose I have a class CTraffic in which several objects of class
CVehicle move around. However, class CVehicle is an abstract base
class, since every object of it is member of subclass CCar or subclass
CBicycle.

So if I define a CVehicle inside CTraffic, I can make an if/else block
that decides if we're on the main road or on the bicycle path. Inside
this block I can then declare and initialize the right object as
CCar* pVehicle = new CCar();
or
CBicycle* pVehicle = new CBicycle();
and afterwards perform some CVehicle functions (like MoveFroward(int
dist)) on my new pointer pVehicle.
---
However, something seams to be wrond with this concept, because
debugging gives an error
error C2065: 'pBac' : undeclared identifier
on the line where I first use the new pVehicle object outside the
if/else block.

For any help or effort: thx in advance!!!

j
 
R

ravips

Hi all,

I'm a newbe, so sorry if this question would be inappropriate here.
Nevertheless I try.
---
Suppose I have a class CTraffic in which several objects of class
CVehicle move around. However, class CVehicle is an abstract base
class, since every object of it is member of subclass CCar or subclass
CBicycle.

So if I define a CVehicle inside CTraffic, I can make an if/else block
that decides if we're on the main road or on the bicycle path. Inside
this block I can then declare and initialize the right object as
CCar* pVehicle = new CCar();
or
CBicycle* pVehicle = new CBicycle();
May be this is what you have to do:

CVehicle * pVehicle;

if( MainRoad() )
pVehicle = new CCar;
else if( BicyclePath() )
pVehicle = new CBicycle;

pVehicle->MoveFroward();
and afterwards perform some CVehicle functions (like MoveFroward(int
dist)) on my new pointer pVehicle.

where is pBac decalred?
on the line where I first use the new pVehicle object outside the
if/else block.

For any help or effort: thx in advance!!!

j

Ravi
 
J

John Harrison

Hi all,

I'm a newbe, so sorry if this question would be inappropriate here.
Nevertheless I try.
---
Suppose I have a class CTraffic in which several objects of class
CVehicle move around. However, class CVehicle is an abstract base
class, since every object of it is member of subclass CCar or subclass
CBicycle.

So if I define a CVehicle inside CTraffic, I can make an if/else block
that decides if we're on the main road or on the bicycle path. Inside
this block I can then declare and initialize the right object as
CCar* pVehicle = new CCar();
or
CBicycle* pVehicle = new CBicycle();
and afterwards perform some CVehicle functions (like MoveFroward(int
dist)) on my new pointer pVehicle.
---
However, something seams to be wrond with this concept, because
debugging gives an error
error C2065: 'pBac' : undeclared identifier
on the line where I first use the new pVehicle object outside the
if/else block.

For any help or effort: thx in advance!!!

j

I think it's simple enough, you have this

if (something)
CCar* pVehicle = new CCar();
else
CBicycle* pVehicle = new CBicycle();

Those vehicles only exist inside the if else statement. What you want is
this

CVehicle* pVehicle;
if (something)
pVehicle = new CCar();
else
pVehicle = new CBicycle();

now because pVehicle is declared outside the if else statement it exists
outside the if else statement.

Note the type of pVehicle has changed to your abstract base class which
is how it should be.

john
 
J

John Ratliff

Hi all,

I'm a newbe, so sorry if this question would be inappropriate here.
Nevertheless I try.
---
Suppose I have a class CTraffic in which several objects of class
CVehicle move around. However, class CVehicle is an abstract base
class, since every object of it is member of subclass CCar or subclass
CBicycle.

So if I define a CVehicle inside CTraffic, I can make an if/else block
that decides if we're on the main road or on the bicycle path. Inside
this block I can then declare and initialize the right object as
CCar* pVehicle = new CCar();
or
CBicycle* pVehicle = new CBicycle();
and afterwards perform some CVehicle functions (like MoveFroward(int
dist)) on my new pointer pVehicle.
---
However, something seams to be wrond with this concept, because
debugging gives an error
error C2065: 'pBac' : undeclared identifier
on the line where I first use the new pVehicle object outside the
if/else block.

For any help or effort: thx in advance!!!

Nothing wrong with your concept per se, but you have a definite problem
compiling your code. The compiler has no idea what a pBac is. Neither do
I since you didn't mention it aside from your compile error.

If you want real help, you'll need to post your complete code. Can't
diagnose problems from a vague description.

--John Ratliff
 
I

Ian

---
However, something seams to be wrond with this concept, because
debugging gives an error
error C2065: 'pBac' : undeclared identifier
on the line where I first use the new pVehicle object outside the
if/else block.
Without the code, it sounds like you are declaring the variable inside
an if{} block, so its scope is restricted to that block.

All this CThis and CThat looks horrible by the way.

Ian
 
J

jeroendeurinck

Ok,

I should admit that these are not the real classes, but I used them to
make the picture clear. The error should be
error C2065: 'pVehicle' : undeclared identifier

I already tried the suggestion some of you proposed; this does not work
however for this reason (and now, Ill give you the full code):

-----
(...)
CBac* pBac;
// Create a new bacterium object
if (pBiomass->Morphotype == 0){
pBac = new CFlocForm();
}else if (pBiomass->Morphotype == 1){
pBac = new CFilForm();
// create the new filament
CFilament* pFilament = new CFilament();
// put bacterium in filament body
pFilament->Body.Add(pBac);
// store position in filament
pBac->PosInFilament = 0;
}
(...)
-----
So pBac is a filamentforming or a flocfoming bacterium. However,
filamentforming bacteria have an additional property (PosInFil) and
should be add to an object pFilament which is defined as an array of
filamentforming bacteria.
And now, of course, the error becomes:
error C2664: 'CArray<TYPE,ARG_TYPE>::Add' : cannot convert parameter 1
from 'CBac *' to 'CFilForm *'
because no object declared as CBac-object can be added to pFilament.

Again,
many thanx for any reaction

j
 
R

ravips

CBac is the base class of CFlocForm and CFilForm.
try adding this after doing a dynamic cast...
CFilForm* pFilForm = dynamic_cast<CFilForm*>(pBac);
if( pBac != NULL)
pFilament->Add(pFilForm);
 
R

ravips

typo.................
read it as..
if( pFilForm != NULL)
pFilament->Add(pFilForm);

hope this will solve ur problem
 
J

jeroendeurinck

Ok, thank you ravips,
it seams to work.

However, I still get a warning (it's just a warning):
warning C4541: 'dynamic_cast' used on polymorphic type 'CBac' with
/GR-; unpredictable behavior may result
Should I care about this?

j
 
M

Markus Moll

Hi
CBac is the base class of CFlocForm and CFilForm.
try adding this after doing a dynamic cast...
CFilForm* pFilForm = dynamic_cast<CFilForm*>(pBac);
if( pBac != NULL)
pFilament->Add(pFilForm);

May I suggest using a static_cast? It fits better here...
Additionally, there is no need to check for pBac being a null pointer.
Maybe it would be even better to avoid casting in the first place and have
something like

CFilForm *pCffBac = new CFilForm();
pBac = pCffBac;
[...]
pFilament->Body.Add(pCffBac);

I think it's a question of taste...

Markus
 
R

ravips

check wheteher you have enabled RTTI or not.
Go to project options C/C++ tab and select C++ Language. There check
"enable RTTI" and your warning should go-off.
After dynamic cast you need to check for null, the casting is proper
only if the object pointed is of correct type(casted one), other wise
it returns null.
adn throws an exception bad_cast.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top