I have a base class with a public method, but when I try to call it from a derived class which inherits publicly from the base class it becomes private. How is it possible? Shouldn't public inheritance means that public methods are kept public?
These is the base class and the method I am referring to is the method r()
specifically
#define POTENTIALS_HPP
#include <cmath>
#include "Matrix.hpp"
#include "Physics_constants.hpp"
#include "PotentialData.hpp"
class Potential {
public:
Potential() {}
Potential(int l, int s, int j, double Mmu) : m_l(l), m_s(s), m_j(j),
m_Mmu(Mmu){}
virtual void setL(int) = 0;
virtual void setS(int) = 0;
virtual void setJ(int) = 0;
virtual void setR(double) = 0;
ushort l() const {return m_l;}
ushort s() const {return m_s;}
ushort j() const {return m_j;}
double r() const {return m_r;}
double Mr() const {return m_Mmu;}
~Potential() {}
virtual Matrix<double> operator () () const = 0;
virtual Matrix<double> operator () (double) = 0;
Matrix<double> Veff() const {
if (m_l==0 || m_r == 0)
return (*this)();
Matrix<double> p = (*this)();
Matrix<double> L(p.cols());
for (uint i=0; i<L.cols(); i++)
L[i][i] = (m_l+2*i)*(m_l+2*i+1);
L = L*(pow(Physics::Nuclear::hc,2)/(2*m_Mmu*m_r*m_r));
std::cout << m_Mmu << " " << m_r << std::endl;
std::cout << p-L << std::endl;
abort();
return p-L;
}
Matrix<double> Veff(double r) {
setR(r);
return Veff();
}
protected:
ushort m_l;
ushort m_s;
ushort m_j;
double m_r;
double m_Mmu;
};
#endif // POTENTIALS_HPP
and this is the derived class
#ifndef ALPHAD_POTENTIAL_HAMMACHE_HPP
#define ALPHAD_POTENTIAL_HAMMACHE_HPP
#include "Physics_constants.hpp"
#include "Potentials.hpp"
#include <cmath>
class HammachePotential : public Potential {
public:
HammachePotential() { m_s = 1; m_Mmu = PotentialData::Hammache::Mr;}
HammachePotential(int l, int j) : Potential(l,1,j,PotentialData::Hammache::Mr) {
if (l==0)
V0 = 60.712;
else
V0 = 56.7;
setLS2(l,s,j);
}
void setS(int) override {m_s=1;}
void setL(int l) override {
this->l = l;
if (l==0)
V0 = 60.712;
else
V0 = 56.7;
setLS2(l,s,j);
}
void setJ(int j) override {
this->j = j;
setLS2(l,s,j);
}
void setR(double r) override {
if (r==0)
return;
this->r = r;
EXPR = expr(r);
EXPRM1 = exprm1(EXPR);
}
std::tuple<int,int,int,double> getInfo() const {
return {l,s,j,r};
}
double getVcDepth() const {
return V0;
}
Matrix<double> operator () () const {
if (r==0)
return Matrix<double>(1);
double Vc = -V0*EXPRM1;
double Ve = Vem(r);
double Vso= VSO*EXPR*std::pow(EXPRM1,2)/r;
Matrix<double> ret(1);
ret[0][0] = Vc+Ve+Vso;
return ret;
}
Matrix<double> operator () (double r) {
if (r==0)
return Matrix<double>(1);
setR(r);
double Vc = -V0*EXPRM1;
double Ve = Vem(r);
double Vso= VSO*EXPR*std::pow(EXPRM1,2)/r;
Matrix<double> ret(1);
ret[0][0] = Vc+Ve+Vso;
return ret;
}
private:
const double a = 0.65; //fm
const double r0 = 1.25; //fm
const double R = r0*std::pow(6.,1./3); //fm
const double l2 = 4.; //fm^2 -> lambda^2
int l = 0;
const int s = 1;
int j = 1;
int LS2;
double V0;
const double V1 = 2.4; //MeV
const double coul = 2*Physics::alpha*Physics::Nuclear::hc;
const double VSOfac0 = -V1*l2/a;
double VSO;
double r;
double EXPR;
double EXPRM1;
private:
inline void setLS2(int l, int s, int j) {
LS2 = j*(j+1) - l*(l+1) - s*(s+1);
VSO = VSOfac0*LS2;
}
inline double Vem(double r) const {
if (r<=R)
return coul*(3-std::pow(r/R,2))/(2*R);
else
return coul/r;
}
inline double expr(double r) const {
return std::exp( (r-R)/a );
}
inline double exprm1 (double expr) const {
return 1.0/(1.0 + expr);
}
};
#endif // ALPHAD_POTENTIAL_HAMMACHE_HPP
Edit: The compiler output is
...main.cpp:184: error: ‘double HammachePotential::r’ is private within this context
...main.cpp:184:30: error: ‘double HammachePotential::r’ is private within this context
184 | std::cout << pot.r() << std::endl;
| ^```
The error is also for the methods `l()`, `s()`, `j()`, but not `Mr()`.
I will soon try to reproduce a minimal example of it and see how it goes. I am sorry, but I am new asking questions here.
you have a private member variable r
in HammachePotential
which shadows the method in Potential
. Make sure to check the full error message from your compiler, GCC at least gives enough hints to work this out:
https://godbolt.org/z/bbv5v9feh
<source>:188:7: error: 'double HammachePotential::r' is private within this context
188 | a.r();
| ^
<source>:162:12: note: declared private here
162 | double r;
| ^
<source>:188:8: error: expression cannot be used as a function
188 | a.r();
| ~~~^~