为什么助推构造器的工作方式如此不同?

Why do the boost-constructors work so different?

本文关键字:方式如 工作 构造器 为什么      更新时间:2023-10-16

我想在我的类中定义我的boost-distribution对象,并继续使用它们。

对于二项分布,这不是问题。

Bdist.hpp

#include "boost/math/distributions/binomial.hpp"
class Bdist {
public:
    Bdist();
    Bdist(unsigned n, double theta);
    virtual ~Bdist(){};
    /**stuff**/
private:
    boost::math::binomial_distribution<> binomialboost;
    double theta; //Every experiment successes with this propability 
    unsigned n; //Amount of trials
};

Bdist.cpp

Bdist::Bdist(unsigned n, double theta) :
n(n), theta(theta) {
    binomialboost= boost::math::binomial_distribution<> (((int)n),theta); 
}
Bdist::Bdist() {
    n = 0;
    theta = 0.0;
    binomialboost = boost::math::binomial_distribution<>(((int)n), theta);
}

奇怪的是,当我对几何分布做同样的处理时,它失败了:

Gdist::Gdist() {
    theta = 0;
    geometricboost = boost::math::geometric_distribution<>(theta);
}
Gdist::Gdist(double theta) :
theta(theta) {
    geometricboost = boost::math::geometric_distribution<>(theta);
}

Gdist.hpp

#include <complex>
#include <boost/math/distributions/geometric.hpp>
class Gdist {
public:
    Gdist();
    Gdist(double theta);
    virtual ~Gdist(){};
/**stuff**/
private:
    boost::math::geometric_distribution <> geometricboost;
    double theta; //Propability
};
为了测试的目的,我写了一个小的main.cpp来看看它对不同的初始化是如何反应的:
#include <cstdlib>
#include <boost/math/distributions/geometric.hpp>
int main(int argc, char** argv) {
boost::math::geometric_distribution<> geoboost;    //fails here
geoboost = boost::math::geometric_distribution<double>(0.1);
printf("%f",boost::math::pdf(geoboost, 0.5));
    return 0;
}

这里我得到:

main.cpp:18:39: error: no matching function for call to ‘boost::math::geometric_distribution<double>::geometric_distribution()’
 boost::math::geometric_distribution<> geoboost;
                                       ^
boost::math::geometric_distribution<double> geoboost;      //Error still here
geoboost = boost::math::geometric_distribution<double>(0.1);

消息没有好转:

main.cpp:18:45: error: no matching function for call to ‘boost::math::geometric_distribution<double>::geometric_distribution()’
 boost::math::geometric_distribution<double> geoboost;
                                             ^

binomial_distribution和geometric_distribution的定义并没有太大的不同:

template <class RealType = double, class Policy = policies::policy<> >
    class binomial_distribution
    {
    public:
      typedef RealType value_type;
      typedef Policy policy_type;
      binomial_distribution(RealType n = 1, RealType p = 0.5) : m_n(n), m_p(p)

template <class RealType = double, class Policy = policies::policy<> >
    class geometric_distribution
    {
    public:
      typedef RealType value_type;
      typedef Policy policy_type;
      geometric_distribution(RealType p) : m_p(p)

这是怎么回事?为什么一个失败而另一个没有?

显式初始化分布,而不是默认初始化它然后对其赋值,例如

Gdist::Gdist(double theta) :
  theta(theta), geometricboost(theta) {
}

二项分布的参数是默认的,也就是说,boost::math::binomial_distribution<> binomialboost;是一个合法的成员,因为它可以默认构造。

几何分布需要参数

问题是由于您没有正确初始化它们,只是将其留给默认构造函数,然后立即覆盖该值而引起的。只要在初始化列表中正确地初始化它们,问题就会解决。

相关文章: