为什么 boost-constructors 的工作方式如此不同?
Why do the boost-constructors work so different?
我想在我的 class 中定义我的 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;
^
通过为模板插入 double...
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)
怎么会这样?为什么一个失败,另一个失败?
二项分布的参数是默认的-也就是说boost::math::binomial_distribution<> binomialboost;
本身就是一个合法的成员,因为它可以被默认构造。
几何分布需要一个参数。
问题是由于你没有正确初始化它们而只是将其留给默认构造函数,然后立即覆盖值造成的。只需在初始化列表中正确初始化它们,问题就会消失。
显式初始化分布,而不是默认初始化然后分配给它,例如
Gdist::Gdist(double theta) :
theta(theta), geometricboost(theta) {
}
我想在我的 class 中定义我的 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;
^
通过为模板插入 double...
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)
怎么会这样?为什么一个失败,另一个失败?
二项分布的参数是默认的-也就是说boost::math::binomial_distribution<> binomialboost;
本身就是一个合法的成员,因为它可以被默认构造。
几何分布需要一个参数。
问题是由于你没有正确初始化它们而只是将其留给默认构造函数,然后立即覆盖值造成的。只需在初始化列表中正确初始化它们,问题就会消失。
显式初始化分布,而不是默认初始化然后分配给它,例如
Gdist::Gdist(double theta) :
theta(theta), geometricboost(theta) {
}