469,904 Members | 2,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

Conditionally initiating objects

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

Oct 20 '05 #1
10 1467

jeroendeuri...@yahoo.com wrote:
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.
---
However, something seams to be wrond with this concept, because
debugging gives an error
error C2065: 'pBac' : undeclared identifier
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

Oct 20 '05 #2
je************@yahoo.com wrote:
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
Oct 20 '05 #3
je************@yahoo.com wrote:
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
Oct 20 '05 #4
Ian
je************@yahoo.com wrote:
---
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
Oct 20 '05 #5
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

Oct 20 '05 #6
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);

Oct 20 '05 #7
typo.................
read it as..
if( pFilForm != NULL)
pFilament->Add(pFilForm);

hope this will solve ur problem

Oct 20 '05 #8
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

Oct 20 '05 #9
Hi

ravips wrote:
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

Oct 20 '05 #10
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.

Oct 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Don | last post: by
17 posts views Thread by Mark | last post: by
2 posts views Thread by Olav Tollefsen | last post: by
2 posts views Thread by Jens Weiermann | last post: by
2 posts views Thread by Felix | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.