开发者

Boost Random with templates

So I'm trying to use the Boost.Random mt19937 generator with templates. My c++ is a bit rusty, but from all I understand (and the doc, as always for Boost, is no less than vague) it should take a template argument that specifies it's return type (float / double).

I have no idea right now as to where the problem lies... It all worked with <double> or <float> and stopped working with the template.

Here's the code:

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();
}

This throws up at the variate_generator construction with

/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

As I said, it's been some time since I've done c++, and the Boost doc leaves much to ask for, so any hints apprecia开发者_运维问答ted...


generateNumber cannot be const - it permutes the Mersenne Twister. Make that mutable, or make the function non-const.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜