473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1630

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Don | last post by:
Given a page with a "header" and "main" frame, is there some way from the "header" frame to initiate re-execution of the JavaScrip/HTML code in the "main" frame? I don't want to have to reload...
17
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class...
10
by: SueB | last post by:
I currently have a 'mail-merge' process in my Access db project. It generates custom filled out Award Certificates based on an SQL SELECT statement in a VBA routine invoked by clicking on a...
2
by: Olav Tollefsen | last post by:
I'm creating an ASP.NET e-commerce web site and I have to conditionally (depending on site / user settings) display prices either excluding or including tax. Prices are typically read from a...
2
by: Jens Weiermann | last post by:
Hi, I need to conditionally disable a html (not web forms) button control. The condition is to be evaluated server-side. What is the smartest way to do this? I'm now using a literal server-side...
2
by: Felix | last post by:
Hello, I use a #define like WITH_MYSTUFF to conditionally compile code within #ifdef WITH_MYSTUFF. I would now like the same define to conditionally link with a library. In the linker settings...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
4
by: bubulle | last post by:
Hi, all. Here is my problem: Let's say i have table1 with columns a,b,c and table2 with cols x,y,z. Some of columns contain the same type of data from one table to the other, but others are...
0
by: AmyHanson | last post by:
I am new to mobile development and am having some trouble sources for the tasks I need to perform. I have been looking around and I can find plenty of information on copying the file initiating on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
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...

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.