Conditionally initiating objects

Discussion in 'C++' started by jeroendeurinck@yahoo.com, Oct 20, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. ravips Guest

    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
    ravips, Oct 20, 2005
    #2
    1. Advertising

  3. 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
    John Harrison, Oct 20, 2005
    #3
  4. John Ratliff Guest

    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
    John Ratliff, Oct 20, 2005
    #4
  5. Ian Guest

    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
    Ian, Oct 20, 2005
    #5
  6. Guest

    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, 2005
    #6
  7. ravips Guest

    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);
    ravips, Oct 20, 2005
    #7
  8. ravips Guest

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

    hope this will solve ur problem
    ravips, Oct 20, 2005
    #8
  9. Guest

    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, 2005
    #9
  10. Markus Moll Guest

    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
    Markus Moll, Oct 20, 2005
    #10
  11. ravips Guest

    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.
    ravips, Oct 20, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay
    Replies:
    8
    Views:
    6,525
    livshitz
    Jun 12, 2011
  2. Pavils Jurjans

    ASP page initiating complex code execution

    Pavils Jurjans, Feb 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    350
    Tommy
    Feb 3, 2004
  3. Replies:
    1
    Views:
    337
    souri challa
    Jul 8, 2005
  4. Replies:
    2
    Views:
    340
  5. John
    Replies:
    2
    Views:
    1,955
    Mark Newmister
    Feb 28, 2006
Loading...

Share This Page