473,698 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1658

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->PosInFilamen t = 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,AR G_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<CF ilForm*>(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<CF ilForm*>(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(pCffB ac);

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

Markus

Oct 20 '05 #10

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

Similar topics

4
1320
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 "main". Thanks in advance. Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= East/West-Coast Server Farms - Total...
17
2668
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 TileMap { public:
10
2476
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 command button. The "problem": I want to conditionally insert some text into the award certificate based on a field selected by the SELECT statement. Is this possible? Details: One of the fields selected is a concatenation of a value from a table
2
1337
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 database (where also the tax rates are stored) various places in the code and typically (but now always) the values are returned in DataSets which are bound to controls and displayed to the user. What would be the most elegant way to implement the...
2
2709
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 control, conditionally placing the complete html code into the text property a lá If (SomeCondition) literal1.Text = "<INPUT type=\"button\" value=\"Something\">";
2
1301
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 of VS.NET 2003 I have 'Input -> Additional Dependencies' where I can add my .lib files the linker should use. How do I tell the linker (by a macro ?) somthing like this:
10
3439
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 dynamic type of 'input' is Derived1, then 'output' should become a reference to 'input'. Otherwise 'output' should become a reference to the (temporary) result of the member function 'input.to_der1()' which returns a Derived1 object by value.
4
2869
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 totally different. Both tables have a very large number of rows (~70k). Now, i need to replicate in table1 a column that exists only in table2, so i create a new column in table1, let's name it d, and i want to conditionally import column z from table2...
0
2499
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 the PC end, I need it to initiate on the Mobile Device end. I need the handheld to be the device that is initiating the file copy as it is the only one that knows the name of the file(s) to be copied. I have found numerous references to using RAPI...
0
8611
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
9170
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
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7741
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
5867
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
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
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
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.