c++中没有匹配的函数:给调用函数添加常量标签

no matching function in C++: constant tag added to calling function

本文关键字:函数 调用 添加 标签 常量 c++      更新时间:2023-10-16

我刚刚开始我在c++领域的冒险,所以这可能是个愚蠢的问题。我从我的编译器得到以下错误。

运行。cc:56:错误:调用' sphereDetect::getArrayPtr() const '没有匹配的函数/spheredetect。hh:18:注:候选值为:const G4long (* sphereDetect::getArrayPtr())[36][72][60]

我的运行。hh:

#include "spheredetect.hh"
#include "G4Run.hh"
#include "globals.hh"
class G4Event;
/// Run class
///
class Run : public G4Run
{
  public:
    Run();
    virtual ~Run();
    // method from the base class
    virtual void Merge(const G4Run*);
    void AddEdep (G4double edep);
    // get methods
    G4double GetEdep()  const { return fEdep; }
    G4double GetEdep2() const { return fEdep2; }
  private:
    G4double  fEdep;
    G4double  fEdep2;
    sphereDetect scatter;
};

我的运行。cc是:

#include "Run.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Run::Run()
: G4Run(),
  fEdep(0.),
  fEdep2(0.),
  scatter()
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Run::~Run()
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void Run::Merge(const G4Run* run)
{

  const Run* localRun = static_cast<const Run*>(run);
  fEdep  += localRun->fEdep;
  fEdep2 += localRun->fEdep2;
  arr* scatterPointer = localRun->scatter.getArrayPtr();
  scatter.sphereMerge(scatterPointer);
  G4Run::Merge(run);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void Run::AddEdep (G4double edep)
{
  fEdep  += edep;
  fEdep2 += edep*edep;
}

和我的sphereDetect。hh:

typedef G4long arr[36][72][60];
class sphereDetect
{
public:
    sphereDetect();
     ~sphereDetect();
      const arr* getArrayPtr() {return &scatterArray;}
     void sphereMerge(arr*);
     void createHit(G4ThreeVector,G4double);
protected:
     void storeHit(G4int,G4int,G4int);
     G4int findAngNS(G4ThreeVector);
     G4int findAngEW(G4ThreeVector);
     G4int findEnergy(G4double);
     void sphereSave();
private:

    G4long scatterArray[36][72][60];
};

我完全不知道如何解决这个问题。它是在我构造或调用sphereDetect类的方式?有一件事是肯定的,那就是Run的输入。需要Merge作为该输入(基于前面的代码)。

任何帮助都非常感谢,

你缺一个const

 const arr* getArrayPtr() {}

表示"this返回一个const指针"。

 const arr* getArrayPtr() const {}

表示"this返回一个const指针,并且可以在const对象上调用"。否则,编译器无法判断getArrayPtr()不想修改调用它的对象。

由于localRunconst,它的成员localRun->scatter也是const

编译器正在寻找getArrayPtr()的定义,该定义声明它不会修改对象,因此可以安全地调用const对象。这是通过将const放在函数签名的其余部分之后来完成的,就像G4double GetEdep() const一样。