Can't see the "if"

Hello, I’m trying to add flags to my simulation.

I’m having a problem using an if.

In my exampleB1.cc I wrote

G4bool CircularTargetFlag=true; //switching on this flag simulation uses circular target

in my B1DetectorConstruction.hh

private:
		G4bool fCircularTargetFlag;

and in the B1DetectorConstructio.cc

B1DetectorConstruction::B1DetectorConstruction(G4bool CircularTargetFlag, G4bool SecondTargetFlag, G4int TargMat, G4double pRMin, G4double pRMax, G4double pDz, G4double pSPhi, G4double pDPhi)
: G4VUserDetectorConstruction(),
  fCircularTargetFlag(CircularTargetFlag), 

and

    if (fCircularTargetFlag){
  		
		G4Tubs* solidEnv =    
    		new G4Tubs("Circular",                    //its name
            fpRMin, fpRMax, 0.5*fpDz, fpSPhi, fpDPhi);    //its size
	}

but when I try to compile and run I get this error

159:27: error: ‘solidEnv’ was not declared in this scope
       new G4LogicalVolume(solidEnv,            //its solid
                           ^~~~~~~~
/data_collamaf/DataFausto/Muon_Collider/II_anno/Simulazione_Geant/2021/231-10-05/B1/src/B1DetectorConstruction.cc:159:27: note: suggested alternative: ‘logicEnv’
       new G4LogicalVolume(solidEnv,            //its solid
                           ^~~~~~~~

and I don’t understand the reason…

fCircularTargetFlag is initializated fo CircularTargetFlag value by the line
fCircularTargetFlag(CircularTargetFlag),

then if (fCircularTargetFlag){
should work and create the solid…but it looks like that the if doesn’t work…

B1DetectorConstruction.hh (2.7 KB)
exampleB1.cc (4.6 KB)
B1DetectorConstruction.cc (7.5 KB)

you are closing the bracket in line 156 (instead of maybe 185 where according to the indent it was supposed to be?) in B1DetectorConstruction.cc.
solidEnv is only defined in the scope of the if-clause…

Hello @weller I closed the brackets because I have several target shapes…then the flag is to choose the target shape…
That’s because I need the if…I mean that I will add to the code something like

if (fMeniscusTargetFlag){

if (fHourglassTargetFlag){

etc.

so I need that the solidEnv exits out of the bracket if the condition is true…how can I do it?

G4Tubs* solidEnv;
if(fCircularTargetFlag) {
    solidEnv = new G4Tubs(...);
else {
    solidEnv = new G4Tubs(...);
}

similar to what you did with G4Material in line 114 :wink:

Thank you weller! It works!