我正在一个由许多.cxx
文件组成的项目中。现在,我要编辑一个添加一些变量和函数的类(以下是注释的行)
#ifndef TN_LORENTZCHARGEVECTOR_
#define TN_LORENTZCHARGEVECTOR_
#include<iostream>
#include <TLorentzVector.h>
#include "common/Helpers.h"
class LorentzChargeVector : public TLorentzVector
{
public:
inline LorentzChargeVector()
: TLorentzVector(),
_charge(0) {}
inline LorentzChargeVector(const TLorentzVector &vec,
float charge)
: TLorentzVector(vec),
_charge(charge) {}
inline LorentzChargeVector(const LorentzChargeVector &v)
: TLorentzVector(v),
_charge(v.Charge()) {}
inline float Charge() const { return _charge; }
inline void SetCharge(float charge) { _charge = charge; }
/*
inline float Trkd0() {std::cout << "fnction" << _trkd0 << std::endl;return _trkd0;}
inline void SetTrkd0(float trkd0) {std::cout << '\t' << trkd0 << std::endl; _trkd0 = trkd0;std::cout << '\t' << _trkd0 << std::endl;}
inline float Trkd0sig() {return _trkd0sig;}
inline void SetTrkd0sig(float trkd0sig) {_trkd0sig = trkd0sig;}
inline float Trkz0() {return _trkz0;}
inline void SetTrkz0(float trkz0) {_trkz0 = trkz0;}
inline float Trkz0sintheta() {return _trkz0sintheta;}
inline void SetTrkz0sintheta(float trkz0sintheta) {_trkz0sintheta = trkz0sintheta;}
*/
inline LorentzChargeVector &operator=(const LorentzChargeVector &q)
{
TLorentzVector::operator=(q);
_charge = q.Charge();
return *this;
}
inline LorentzChargeVector operator+(const LorentzChargeVector &q) const
{
return LorentzChargeVector(TLorentzVector::operator+(q), _charge + q.Charge());
}
inline LorentzChargeVector &operator+=(const LorentzChargeVector &q)
{
TLorentzVector::operator+=(q);
_charge += q.Charge();
return *this;
}
inline LorentzChargeVector operator-(const LorentzChargeVector &q) const
{
return LorentzChargeVector(TLorentzVector::operator-(q), _charge - q.Charge());
}
inline LorentzChargeVector &operator-=(const LorentzChargeVector &q)
{
TLorentzVector::operator-=(q);
_charge -= q.Charge();
return *this;
}
inline LorentzChargeVector operator-() const
{
return LorentzChargeVector(TLorentzVector::operator-(), -_charge);
}
inline Bool_t operator==(const LorentzChargeVector &q) const
{
return (Vect() == q.Vect() && T() == q.T() && Charge() == q.Charge());
}
inline Bool_t operator!=(const LorentzChargeVector &q) const
{
return (Vect() != q.Vect() || T() != q.T() || Charge() != q.Charge());
}
// Print
inline void Print(Option_t *option="") const override
{
UNUSED(option)
Printf("(x,y,z,t)=(%f,%f,%f,%f) (P,eta,phi,E)=(%f,%f,%f,%f) charge=%f",
X(), Y(), Z(), E(),
P(), Eta(), Phi(), E(), _charge);
}
private:
float _charge;
// float _trkd0;
// float _trkd0sig;
// float _trkz0;
// float _trkz0sintheta;
ClassDefOverride(LorentzChargeVector, 1);
};
#endif // TN_LORENTZCHARGEVECTOR_
我的问题在这里由返回值的函数给出。设置值(SetTrkd0
ecc。)的功能可以正常工作。相反,返回值(Trkd0
,Trkd0sig
等)的函数会给我一个任意值。
我认为该错误是由于构造函数引起的,它们没有被编辑为新变量。
答案 0 :(得分:2)
您的构造函数需要显式初始化所有成员,否则它将存储不确定的值,这些值被您视为垃圾(任意值)。对于您的情况,_trkd0
,_trkd0sig
,_trkz0
,_trkz0sintheta
未初始化。
此外,请考虑将类的实现和声明分开。它可能使您可以独立于界面来更改实现,并且在像您这样的情况下处理更改要求时,这可能会更容易。