Error in DetectorConstruction.cc

these are the error i keep on getting while i already setup and included “G4SystemOfUnits.hh” /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc: In member function ‘virtual G4VPhysicalVolume* DetectorConstruction::Construct()’:
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:31:47: error: ‘cm’ was not declared in this scope; did you mean ‘CLHEP::cm’?
31 | G4Box* solidBox = new G4Box(“Box”, 10.0 * cm, 10.0 * cm, 10.0 * cm);
| ^~
| CLHEP::cm
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:100:27: note: ‘CLHEP::cm’ declared here
100 | static constexpr double cm = centimeter;
| ^~
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:34:59: error: ‘g’ was not declared in this scope; did you mean ‘CLHEP::g’?
34 | G4Material* mixture = new G4Material(“Mixture”, 2.5 * g / cm3, 8);
| ^
| CLHEP::g
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:197:29: note: ‘CLHEP::g’ declared here
197 | static constexpr double g = gram;
| ^
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:34:63: error: ‘cm3’ was not declared in this scope; did you mean ‘CLHEP::cm3’?
34 | G4Material* mixture = new G4Material(“Mixture”, 2.5 * g / cm3, 8);
| ^~~
| CLHEP::cm3
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:102:27: note: ‘CLHEP::cm3’ declared here
102 | static constexpr double cm3 = centimeter3;
| ^~~
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:35:39: error: ‘perCent’ was not declared in this scope; did you mean ‘CLHEP::perCent’?
35 | mixture->AddMaterial(nickel, 20 * perCent);
| ^~~~~~~
| CLHEP::perCent
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:319:27: note: ‘CLHEP::perCent’ declared here
319 | static constexpr double perCent = 0.01 ;
| ^~~~~~~
make[2]: *** [CMakeFiles/MyProject.dir/build.make:76: CMakeFiles/MyProject.dir/src/DetectorConstruction.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/MyProject.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Hello @BashirAminu

Please add

#include “CLHEP/Units/SystemOfUnits.h”
using CLHEP::cm;

before constructor
DetectorConstruction::DetectorConstruction()
:G4VUserDetectorConstruction(), …

VRS

i did’t get you. do you mean like this?
#include “DetectorConstruction.hh”

#include “CLHEP/Units/SystemOfUnits.hh”
DetectorConstruction::DetectorConstruction() {
// Constructor
}

DetectorConstruction::~DetectorConstruction() {
// Destructor
}

i did this still not working

Hello @BashirAminu

#include “DetectorConstruction.hh”
#include “CLHEP/Units/SystemOfUnits.hh”
using CLHEP::cm; // You might forget to add this.

DetectorConstruction::DetectorConstruction() {
// Constructor
}

VRS

#include “DetectorConstruction.hh”

#include “CLHEP/Units/SystemOfUnits.h”
DetectorConstruction::DetectorConstruction() {
// Constructor
}

DetectorConstruction::~DetectorConstruction() {
// Destructor
}

G4VPhysicalVolume* DetectorConstruction::Construct() {
// Get NIST Manager
G4NistManager* nistManager = G4NistManager::Instance();

// Define materials
G4Material* nickel = nistManager->FindOrBuildMaterial("G4_Ni");
G4Material* iron = nistManager->FindOrBuildMaterial("G4_Fe");
G4Material* rhenium = nistManager->FindOrBuildMaterial("G4_Re");
G4Material* boron_carbide = nistManager->FindOrBuildMaterial("G4_BORON_CARBIDE");
G4Material* chromium = nistManager->FindOrBuildMaterial("G4_Cr");
G4Material* titanium = nistManager->FindOrBuildMaterial("G4_Ti");
G4Material* copper = nistManager->FindOrBuildMaterial("G4_Cu");
G4Material* tungsten = nistManager->FindOrBuildMaterial("G4_W");

// Define a box shape
G4Box* solidBox = new G4Box("Box", 10.0 * cm, 10.0 * cm, 10.0 * cm);

// Define a logical volume with a mixture of materials
G4Material* mixture = new G4Material("Mixture", 2.5 * g / cm3, 8);
mixture->AddMaterial(nickel, 20 * perCent);
mixture->AddMaterial(iron, 20 * perCent);
mixture->AddMaterial(rhenium, 10 * perCent);
mixture->AddMaterial(boron_carbide, 10 * perCent);
mixture->AddMaterial(chromium, 10 * perCent);
mixture->AddMaterial(titanium, 10 * perCent);
mixture->AddMaterial(copper, 10 * perCent);
mixture->AddMaterial(tungsten, 10 * perCent);

G4LogicalVolume* logicBox = new G4LogicalVolume(solidBox, mixture, "Box");

// Place the box at the center of the world volume
G4VPhysicalVolume* physBox = new G4PVPlacement(0, G4ThreeVector(), logicBox, "Box", 0, false, 0);

return physBox;

}

DetectorConstruction.hh

#ifndef DETECTORCONSTRUCTION_HH
#define DETECTORCONSTRUCTION_HH

#include “G4VUserDetectorConstruction.hh”
#include “G4NistManager.hh”
#include “G4Box.hh”
#include “G4LogicalVolume.hh”
#include “G4PVPlacement.hh”
#include “G4Material.hh”
#include “G4RunManager.hh”
#include “G4SystemOfUnits.hh”

class DetectorConstruction : public G4VUserDetectorConstruction {
public:
DetectorConstruction();
virtual ~DetectorConstruction();

virtual G4VPhysicalVolume* Construct() override;

};

#endif

above is my DetectorConstruction.cc and DetectorConstruction.hh folder. but still not working.

Dear @BashirAminu ,

I checked and found no error. As expected it is cubical box.

image

Your files …
DetectorConstruction.cc (1.7 KB)
DetectorConstruction.hh (489 Bytes)

Best,
VRS

Take errors screen capture. If persist.

VRS

I STILL RECEIVE THIS ERROR
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc: In member function ‘virtual G4VPhysicalVolume* DetectorConstruction::Construct()’:
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:32:47: error: ‘cm’ was not declared in this scope; did you mean ‘CLHEP::cm’?
32 | G4Box* solidBox = new G4Box(“Box”, 10.0 * cm, 10.0 * cm, 10.0 * cm);
| ^~
| CLHEP::cm
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:100:27: note: ‘CLHEP::cm’ declared here
100 | static constexpr double cm = centimeter;
| ^~
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:35:59: error: ‘g’ was not declared in this scope; did you mean ‘CLHEP::g’?
35 | G4Material* mixture = new G4Material(“Mixture”, 2.5 * g / cm3, 8);
| ^
| CLHEP::g
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:197:29: note: ‘CLHEP::g’ declared here
197 | static constexpr double g = gram;
| ^
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:35:63: error: ‘cm3’ was not declared in this scope; did you mean ‘CLHEP::cm3’?
35 | G4Material* mixture = new G4Material(“Mixture”, 2.5 * g / cm3, 8);
| ^~~
| CLHEP::cm3
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:102:27: note: ‘CLHEP::cm3’ declared here
102 | static constexpr double cm3 = centimeter3;
| ^~~
/home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:36:39: error: ‘perCent’ was not declared in this scope; did you mean ‘CLHEP::perCent’?
36 | mixture->AddMaterial(nickel, 20 * perCent);
| ^~~~~~~
| CLHEP::perCent
In file included from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/PhysicalConstants.h:43,
from /home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/G4NistManager.hh:68,
from /home/bashir/Bash/exercise/project1/MyProject/src/DetectorConstruction.cc:2:
/home/bashir/application/geant4/geant4-v11.1.1-install/include/Geant4/CLHEP/Units/SystemOfUnits.h:319:27: note: ‘CLHEP::perCent’ declared here
319 | static constexpr double perCent = 0.01 ;
| ^~~~~~~
make[2]: *** [CMakeFiles/MyProject.dir/build.make:76: CMakeFiles/MyProject.dir/src/DetectorConstruction.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/MyProject.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

BUT THSES ARE THE DETECTORCONSTRUCTION FOLDERS I THINK THE ONE I GAVE YOU BEFORE IS NOT THE EXACT ONE
#include “DetectorConstruction.hh”
#include “G4NistManager.hh”
#include “G4Box.hh”
#include “G4LogicalVolume.hh”
#include “G4PVPlacement.hh”
#include “G4Material.hh”
#include “CLHEP/Units/SystemOfUnits.h”

DetectorConstruction::DetectorConstruction() {
// Constructor
}

DetectorConstruction::~DetectorConstruction() {
// Destructor
}

G4VPhysicalVolume* DetectorConstruction::Construct() {
// Get NIST Manager
G4NistManager* nistManager = G4NistManager::Instance();

// Define materials
G4Material* nickel = nistManager->FindOrBuildMaterial("G4_Ni");
G4Material* iron = nistManager->FindOrBuildMaterial("G4_Fe");
G4Material* rhenium = nistManager->FindOrBuildMaterial("G4_Re");
G4Material* boron_carbide = nistManager->FindOrBuildMaterial("G4_BORON_CARBIDE");
G4Material* chromium = nistManager->FindOrBuildMaterial("G4_Cr");
G4Material* titanium = nistManager->FindOrBuildMaterial("G4_Ti");
G4Material* copper = nistManager->FindOrBuildMaterial("G4_Cu");
G4Material* tungsten = nistManager->FindOrBuildMaterial("G4_W");

// Define a box shape
G4Box* solidBox = new G4Box("Box", 10.0 * cm, 10.0 * cm, 10.0 * cm);

// Define a logical volume with a mixture of materials
G4Material* mixture = new G4Material("Mixture", 2.5 * g / cm3, 8);
mixture->AddMaterial(nickel, 20 * perCent);
mixture->AddMaterial(iron, 20 * perCent);
mixture->AddMaterial(rhenium, 10 * perCent);
mixture->AddMaterial(boron_carbide, 10 * perCent);
mixture->AddMaterial(chromium, 10 * perCent);
mixture->AddMaterial(titanium, 10 * perCent);
mixture->AddMaterial(copper, 10 * perCent);
mixture->AddMaterial(tungsten, 10 * perCent);

G4LogicalVolume* logicBox = new G4LogicalVolume(solidBox, mixture, "Box");

// Place the box at the center of the world volume
G4VPhysicalVolume* physBox = new G4PVPlacement(0, G4ThreeVector(), logicBox, "Box", 0, false, 0);

return physBox;

}

AND
#ifndef DETECTORCONSTRUCTION_HH
#define DETECTORCONSTRUCTION_HH

#include “G4VUserDetectorConstruction.hh”

class DetectorConstruction : public G4VUserDetectorConstruction {
public:
DetectorConstruction();
virtual ~DetectorConstruction();

virtual G4VPhysicalVolume* Construct() override;

};

#endif

The #include is correct, but the using declaration is also needed like:

#include “CLHEP/Units/SystemOfUnits.hh”

using namespace CLHEP;

so that lines that are using CLHEP work:

G4Box* solidBox = new G4Box("Box", 10.0 * cm, 10.0 * cm, 10.0 * cm);

You can also leave out the using namespace CLHEP;, but would then need to write:

// Define a box shape
G4Box* solidBox = new G4Box("Box", 10.0 * CLHEP::cm, 10.0 * CLHEP::cm, 10.0 * CLHEP::cm);

Please don’t suggest screenshots for code/terminal output, they make reviewing the problem and suggesting solutions more awkward. There is a dedicated pinned post on posting code snippets or terminal output using fenced code blocks which is the preferred method

I suggest to replace CLHEP/Units/SystemOfUnits.h by G4SystemOfUnits.hh
It must work

Dear @bmorgan
I Got it. Thanks for the suggestion. :slight_smile:

VRS

thank you all for the help. i used @bmorgan suggestion and it worked-out.
but i have another issue as follows:
/home/bashir/Bash/exercise/project1/MyProject/src/PhysicsList.cc: In member function ‘virtual void PhysicsList::ConstructProcess()’:
/home/bashir/Bash/exercise/project1/MyProject/src/PhysicsList.cc:25:51: error: cannot call member function ‘virtual void G4EmStandardPhysics_option4::ConstructProcess()’ without object
25 | G4EmStandardPhysics_option4::ConstructProcess();
| ^
make[2]: *** [CMakeFiles/MyProject.dir/build.make:89: CMakeFiles/MyProject.dir/src/PhysicsList.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/MyProject.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

This one is also solve, thank you all.