马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
DetectorConstruction.cc的构造函数里初始化的时候,fDetectorMessenger = new DetectorMessenger(this);报错了
程序报错: error: no matching function for call to ‘DetectorMessenger::DetectorMessenger(B1::DetectorConstruction*)’ fDetectorMessenger = new DetectorMessenger(this);
我尝试在G4UImessenger的.hh文件里面也使用命名空间,报了另一个错
在一个类G4UImessenger里是这样的,没有使用命名空间//
#ifndef B1_DETECTORMESSENGER_HH
#define B1_DETECTORMESSENGER_HH
#include "G4UImessenger.hh"
#include "globals.hh"
#include "G4UImessenger.hh"
#include "globals.hh"
class DetectorConstruction;
class DetectorMessenger : public G4UImessenger {
public:
DetectorMessenger(DetectorConstruction *);
~DetectorMessenger();
virtual void SetNewValue(G4UIcommand *, G4String);
private:
G4UIcmdWithADoubleAndUnit *fDetThickCmd;
DetectorConstruction *fDetectorConstruction;
G4UIdirectory *fTestemDir;
G4UIdirectory *fDetDir;
};
#endif //B1_DETECTORMESSENGER_HH
另一个类 DetectorConstruction里面使用了命名空间//
/// \file DetectorConstruction.hh
/// \brief Definition of the B1::DetectorConstruction class
#ifndef B1DetectorConstruction_h
#define B1DetectorConstruction_h 1
#include "G4VUserDetectorConstruction.hh"
#include "globals.hh"
class G4VPhysicalVolume;
class G4LogicalVolume;
class G4Material;
class G4MaterialCutsCouple;
class G4UniformMagField;
class DetectorMessenger;
/// Detector construction class to define materials and geometry.
namespace B1
{
class RunAction;
class DetectorConstruction : public G4VUserDetectorConstruction
{
public:
// DetectorConstruction(RunAction* runAction);
DetectorConstruction();
~DetectorConstruction() override;
G4VPhysicalVolume* Construct() override;
// for change Det Params
void SetDetThickness(G4double);
G4double GetDetThickness() const {return fDetThickness;};
protected:
G4LogicalVolume* fScoringVolume = nullptr;
G4LogicalVolume *fLogicTubeTarget= nullptr;
G4LogicalVolume *fLogicCryst= nullptr;
private:
RunAction* fRunAction = nullptr;
G4double fDetThickness;
void ChangeGeometry();
DetectorMessenger* fDetectorMessenger;
};
}
#endif
然后在DetectorConstruction.cc的构造函数里初始化的时候,fDetectorMessenger = new DetectorMessenger(this);报错了#include "G4LogicalVolumeStore.hh"
#include "DetectorMessenger.hh"
#include "DetectorConstruction.hh"
#include "DetectorMessenger.hh"
namespace B1
{
DetectorConstruction::DetectorConstruction()
:G4VUserDetectorConstruction(),
fLogicTubeTarget(nullptr),
fLogicCryst(nullptr),
fDetectorMessenger(nullptr)
{
// create commands for interactive definition
fDetectorMessenger = new DetectorMessenger(this);
}
}
从你的错误信息来看,问题出在你试图创建一个新的 DetectorMessenger对象,并将 this(即 DetectorConstruction对象)作为参数传递给 DetectorMessenger的构造函数。错误信息表明, DetectorMessenger的构造函数并没有接受一个 B1::DetectorConstruction*类型的参数。
在你给出的代码中, DetectorMessenger的构造函数接受一个 DetectorConstruction*类型的参数,而不是 B1::DetectorConstruction*。这意味着,你的 DetectorMessenger类期望一个全局命名空间下的 DetectorConstruction对象,而不是在 B1命名空间下的。
解决这个问题的一种方法是在 DetectorMessenger的定义中使用 B1::DetectorConstruction*作为构造函数的参数,而不是 DetectorConstruction*。这样, DetectorMessenger就能接受一个 B1::DetectorConstruction*类型的参数了。你需要修改 DetectorMessenger的定义,如下所示:
class B1::DetectorConstruction; // 注意这里的改动
class DetectorMessenger : public G4UImessenger {
public:
DetectorMessenger(B1::DetectorConstruction *); // 注意这里的改动
~DetectorMessenger();
virtual void SetNewValue(G4UIcommand *, G4String);
private:
G4UIcmdWithADoubleAndUnit *fDetThickCmd;
B1::DetectorConstruction *fDetectorConstruction; // 注意这里的改动
G4UIdirectory *fTestemDir;
G4UIdirectory *fDetDir;
};
然后,你需要在 DetectorMessenger的实现中也做相应的修改。这样,你的代码应该就可以正常工作了。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
|