【问题标题】:Why do the boost-constructors work so different? [duplicate]为什么 boost-constructor 的工作方式如此不同? [复制]
【发布时间】:2015-11-01 18:02:10
【问题描述】:

我想在我的班级中定义我的 boost-distributions 对象并继续使用它们。

对于二项分布,这没有问题。

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)

这怎么可能?为什么一个失败而另一个没有?

【问题讨论】:

  • 虽然有可能,但是您介意解释一下我必须如何在我的 hpp 中定义它吗?我应该如何在我的 cpp 中初始化它?

标签: c++ boost constructor initialization


【解决方案1】:

显式初始化分发,而不是默认初始化然后分配给它,例如。

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

【讨论】:

    【解决方案2】:

    二项分布的参数是默认的——也就是说boost::math::binomial_distribution&lt;&gt; binomialboost;是它自己的合法成员,因为它可以被默认构造。

    几何分布需要一个参数。

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

    【讨论】:

    • 那么我应该如何在我的 hpp 中定义它呢?我应该如何在我的 cpp 中初始化它?
    • 标题中的定义无关紧要。在源文件中,只需将参数传递给初始化列表中的构造函数即可。
    • 像这样:geometricboost = boost::math::geometric_distribution(theta); 给我一个error: missing template arguments before ‘(’ tokenno matching function for call to ‘boost::math::geometric_distribution&lt;double&gt;::geometric_distribution()’
    • @Qohelet 您使用初始化器列表,请参见例如this
    • 知道了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2017-11-14
    相关资源
    最近更新 更多