gpt4 book ai didi

c++ - 使用模板 boost 随机性

转载 作者:行者123 更新时间:2023-11-28 08:18:57 26 4
gpt4 key购买 nike

所以我尝试将 Boost.Random mt19937 生成器与模板一起使用。我的 c++ 有点生疏,但据我所知(和文档一样,Boost 的文档也很模糊)它应该采用一个模板参数来指定它的返回类型(float/double)。

我现在不知道问题出在哪里......这一切都适用于 <double><float>并停止使用模板。

代码如下:

template <class T>
class SpikingMatrixHelper {
public:
SpikingMatrixHelper(const int seed);
T generateNumber(const T, const T) const;
private:
boost::mt19937 gen;
};

template <class T>
SpikingMatrixHelper<T>::SpikingMatrixHelper(const int seed) : gen(seed) {}

template <class T>
T SpikingMatrixHelper<T>::generateNumber(const T min, const T max) const {
boost::uniform_real<T> dist(min, max);
boost::variate_generator<boost::mt19937&, boost::uniform_real<T> > g(gen, dist);
return g();
}

这会在 variate_generator 处抛出 build 用

/path/ [line] error: no matching function for call to ‘boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const mt19937&, boost::uniform_real<double>&)’
/path/ [line] note: candidates are:
/usr/include/boost/random/variate_generator.hpp:133:3: note: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, Distribution = boost::uniform_real<double>]
/usr/include/boost/random/variate_generator.hpp:133:3: note: no known conversion for argument 1 from ‘const mt19937 {aka const boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>}’ to ‘boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&’
/usr/include/boost/random/variate_generator.hpp:114:7: note: boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >::variate_generator(const boost::variate_generator<boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18, 3346425566u>&, boost::uniform_real<double> >&)
/usr/include/boost/random/variate_generator.hpp:114:7: note: candidate expects 1 argument, 2 provided

正如我所说,我已经有一段时间没有使用 C++ 了,而且 Boost 文档还有很多要问的地方,所以任何提示都非常感谢...

最佳答案

generateNumber 不能是 const - 它会置换 Mersenne Twister。使该可变,或使该函数成为非const

关于c++ - 使用模板 boost 随机性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6654302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com