【发布时间】: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