为什么助推构造器的工作方式如此不同?
Why do the boost-constructors work so different?
我想在我的类中定义我的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;
是一个合法的成员,因为它可以默认构造。
几何分布需要参数
问题是由于您没有正确初始化它们,只是将其留给默认构造函数,然后立即覆盖该值而引起的。只要在初始化列表中正确地初始化它们,问题就会解决。
相关文章:
- 如何在c++中为模板函数实例创建快捷方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 布尔比较运算符是如何在C++中工作的
- CV_OCL_RUN宏如何在OpenCV(版本3.4.5)的goodFeaturesToTrack实现中工作?
- 如何在C++中以这种方式返回一行文本?
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 当输入在带有空格的单行中给出时,cin 如何在 while 循环中工作?
- 在 Eclipse 中添加库的工作方式是否与在 Visual Studio 中相同?
- 如何以"union-like"方式更改C++向量的数据类型
- 如何以" Safe "的方式获取 C++ 中的当前时间
- 终端如何在 shell 提示符下以编程方式阻止退格,以便您无法在其上退格,例如 $ 表示 bash 或 C:\blah\>
- 如何用数学方式组合数字?
- 如何在 Poco 中将工作 HTTP 代码转换为 HTTPS
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 如何在 curl 代码C++设置授权持有者标头?我得到的授权不足,即使它在命令行上工作
- switch-case语句如何在Arduino/C++中工作
- 这种计算大数的方式如何工作
- 如何以编程方式从工作线程获得父线程和兄弟线程的CPU利用率