c++中没有匹配的函数:给调用函数添加常量标签
no matching function in C++: constant tag added to calling function
我刚刚开始我在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()
不想修改调用它的对象。
由于localRun
是const
,它的成员localRun->scatter
也是const
。
编译器正在寻找getArrayPtr()
的定义,该定义声明它不会修改对象,因此可以安全地调用const
对象。这是通过将const
放在函数签名的其余部分之后来完成的,就像G4double GetEdep() const
一样。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销